Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of available data member from a POD struct in C++

Tags:

c++

the question can sound a bit unusual. Let's take a POD struct:

struct MyStruct
{
   int myInt;
   double myDouble;
   AnotherPOD* myPointer;
};

The compiler knows the list of available data members. Do you know any way to get list of data member name (and type) either at compile time (better) or at run time?

I have a huge amount of POD structs and I would like to automate the creation of operator<<.

I know I could create a parser for the header files, create some files and compile those. However, I am sure the compiler has already had this information and I would like to exploit it.

Any ideas?

Thanks

like image 945
Alessandro Teruzzi Avatar asked Dec 22 '22 21:12

Alessandro Teruzzi


1 Answers

BOOST_FUSION_ADAPT_STRUCT introduces compile-time reflection (which is awesome).

It is up to you to map this to run-time reflection of course, and it won't be too easy, but it is possible in this direction, while it would not be in the reverse :)

like image 116
Matthieu M. Avatar answered Dec 24 '22 11:12

Matthieu M.