Comments (15)
I agree, that would be great, and it is an oversight.
One thing that concerns me is, if the =
initializer is implicit, then you cannot do certain things:
struct my_type {
std::atomic<int> foo = 0; // doesn't compile: invoking deleted copy constructor of std::atomic<int>
};
struct my_type {
std::atomic<int> foo{0}; // works
};
So I'd be tempted to make the with-initializer syntax like this:
struct my_type {
BEGIN_VISITABLES(my_type);
VISITABLE(int, a, =0);
VISITABLE(float, b, =0.0);
VISITABLE(std::string, c);
VISITABLE(std::atomic<int>, d, {0});
END_VISITABLES;
};
What do you think?
from visit_struct.
This should also work even if not ideal with std::atomic<int>
.
struct my_type {
BEGIN_VISITABLES(my_type);
VISITABLE(int, a, 0);
VISITABLE(float, b, 0.0);
VISITABLE(std::string, c);
VISITABLE(std::atomic<int>, d, {0});
END_VISITABLES;
};
What do you think?
from visit_struct.
We can always think about having VISITABLE
, VISITABLE_INIT
and VISITABLE_LISTINIT
.
struct my_type {
BEGIN_VISITABLES(my_type);
VISITABLE_INIT(int, a, 0);
VISITABLE_INIT(float, b, 0.0);
VISITABLE(std::string, c);
VISITABLE_LIST(std::atomic<int>, d, {0});
END_VISITABLES;
};
from visit_struct.
From my point of view the difference is "copy initialization" (using an =
sign with the initializer e.g. int x = 5;
) vs. "direct initialization" e.g. int x{5};
)
It makes a difference if the class is not copyable or something
What would you think about:
VISITABLE_INIT
takes a 3rd argument, and if it does,=
is generated so that copy initialization is used- When direct initialization is needed,
VISITABLE_DIRECT_INIT
can be used, which is the same but doesn't put an=
I could also make it so that VISITABLE
has an optional third argument which is the initializer, but it requires some preprocessor tricks to have optional arguments AFAIR, so it might harm the quality of error messages slightly when the macro is misused. I guess I can try it and see how the error messages look.
from visit_struct.
Yes! You totally got my point. I just called them differently: VISITABLE_INIT
and VISITABLE_LISTINIT
but the idea is the same. I actually like your naming better.
I wouldn't bother too much about merging VISITABLE_INIT
into VISITABLE
, lets just have 3 different macros so the programmer is aware of what is going on.
If you implement this I will be able to use it in my project! Thanks
Note: I believe this is stopping many from using it because the reason to use reflection is most likely because the struct is big, for the same reason you dont want to manually write a constructor that initialize with default values, but you cannot afford to leave the struct uninitialized...
from visit_struct.
I agree, thanks for bringing this up!
from visit_struct.
Could you let me know when the new feature is in place? Thank you
from visit_struct.
Yeah will do, sorry I've been super busy!
from visit_struct.
Just to let you know, I started working on this, going to make some tests and run on some more compilers just to make sure there's no problem :)
from visit_struct.
Thanks. Let me know if you want me to try it as well before you merge the change.
from visit_struct.
@cbeck88 I was looking for a simple standalone library to interface with some large structs (serialization, exposing to gui, ...) and finally came across your's, which looks fantastic, except for the missing initialization in the intrusive syntax. I'm sure you are probably busy, but did you manage to work on this?
from visit_struct.
Hey im really sorry, I just got married on Jan 4th. I will make time for this really soon! Someone once told me, love is the primary obstacle to many open source software projects... 😅
from visit_struct.
Haha, thanks for the quick reply and congratulations 🍾!
No worries, there are more important things in life than initializer lists 😆.
from visit_struct.
In the meantime this has been working well for me:
#define VISITABLE_INIT(TYPE, NAME, VALUE) \
TYPE NAME = VALUE; \
struct VISIT_STRUCT_MAKE_MEMBER_NAME(NAME) : \
visit_struct::detail::member_ptr_helper<VISIT_STRUCT_CURRENT_TYPE, \
TYPE, \
&VISIT_STRUCT_CURRENT_TYPE::NAME> \
{ \
static VISIT_STRUCT_CONSTEXPR const ::visit_struct::detail::char_array<sizeof(#NAME)> & member_name() { \
return #NAME; \
} \
}; \
static inline ::visit_struct::detail::Append_t<VISIT_STRUCT_GET_REGISTERED_MEMBERS, \
VISIT_STRUCT_MAKE_MEMBER_NAME(NAME)> \
Visit_Struct_Get_Visitables__(::visit_struct::detail::Rank<VISIT_STRUCT_GET_REGISTERED_MEMBERS::size + 1>); \
static_assert(true, "")
from visit_struct.
Also added VISITABLE_DIRECT_INIT
, see #14.
from visit_struct.
Related Issues (20)
- Compatibility with cereal HOT 2
- runtime versions of indexed access functions HOT 3
- Allow non-intrusive annotation of template types
- Feature request: optional visitable metadata HOT 2
- if constexpr type deduction in visitor? HOT 2
- Separate parser program for 'intrusive'
- Get member variable pointer at compile-time HOT 1
- Non const visitor function arguments HOT 1
- Figure out what's wrong with `visit_struct` core on appveyor build HOT 1
- Clang: Compilation error with visitable struct include template class
- QA HOT 1
- conan package HOT 1
- compile error with boost fusion HOT 1
- New release? HOT 6
- Recursive Serialization, decltype and is_visitable behaving strangely HOT 7
- Exposing private members HOT 1
- Add the fully visitable test example as a concept?
- Visiting template classes
- Not compiling using Intel 16.0 (with Visual Studio 2013) HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from visit_struct.