Good day to all. The subject is in the title, and the question is as follows: With the help of which data from the top gentlemen can one get as close as possible to reflection, or even realize the possibility of automatic serialization of user types (which include both other user types and basic ones)? Thanks in advance for the answers, advice, recommendations and links to the material.
- There is no reflection in C ++, so everything depends on a specific task, the condition of which includes, among other things, what are the types that need to be serialized. Your question, for good, can be closed, because it is very non-specific. - ixSci
- I do not know what criteria calculate the correctness of what is not. ) And in fact, specifics are enough for correctness - automatic serialization (respectively, iteration over fields to get their values.) - Pavel Drushlyakov
- A convenient option is to take libclang, parse your code, and pull out the list of fields / methods of all necessary classes from there. - HolyBlackCat
|
1 answer
Reflection in C ++ is only in minimal quantities and before adequate serialization even as before the moon. std::tuple is not a topic at all.
- The option with pseudo-
boost.Hanaon macros is implemented inboost.Hana
#include <iostream> #include <string> #include <cstdlib> #include <boost/hana.hpp> template< typename T > void serialize(::std::ostream& output, T const & object) { ::boost::hana::for_each ( ::boost::hana::members(object) , [&](auto const & member) { output << member << std::endl; } ); } struct Person { BOOST_HANA_DEFINE_STRUCT ( Person , (::std::string, name) , (int, age) ); }; int main() { Person john{"John", 30}; serialize(std::cout, john); return 0; } The variant with pseudo-reflection on code generation is implemented for example in Qt MOC.
The variant with pseudo-reflection on standard defects is implemented in the Loophole .
- Well, on macros and in pure C, you can build reflection:
|