Get a list of available data from a POD structure in C ++

the question may sound a little unusual. Take the POD structure:

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

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

I have a huge number of POD structures, and I would like to automate the creation of the <

I know that I can create a parser for header files, create some files and compile them. However, I am sure that the compiler already had this information, and I would like to use it.

Any ideas?

thanks

+4
source share
4 answers

BOOST_FUSION_ADAPT_STRUCT represents reflection at compile time (which is awesome).

It is up to you to display this in a temporary reflection, of course, and it will not be too easy, but it is possible in this direction, while it will not be inverse :)

+5
source

I don't know how to do what you want directly, but you can take a look at clang, which is a compiler implementation that you can use to do other things:

http://clang.llvm.org

I assume that then you can cross the abstract syntax tree that it creates and get the information that you are after.

+3
source

Well, standard C ++ compilers cannot do this, they lack the ability to reflect.

Sounds like a task to a code generator. Therefore, either use the toolkit to extract this data from the headers, or create both headers and serialization functions from another source. Just make sure you are not repeating .

+2
source

I'm afraid, but C ++ does not support reflection. You can use Boost.TypeTraits to achieve a limited form of reflection at compile time.

+1
source

All Articles