2

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

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
Alessandro Teruzzi
  • 3,918
  • 1
  • 27
  • 41
  • possible duplicate of [How can I add reflection to a C++ application?](http://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application) – Björn Pollex Nov 12 '10 at 16:57
  • You could probably get something working using tricky templates and adding some macros like `REGISTER_MEMBER(MyStruct::myInt)`. But the existing tools pointed out by answers below would probably be less painful. – aschepler Nov 12 '10 at 17:03
  • Are you outputting in binary or text? I assume you're probably outputting text since you're using operator<<, but in case you're not, there's a very simple solution. – Benjamin Lindley Nov 12 '10 at 17:14
  • I need the text version, however I am curious. Which is the very simple solution suitable for binary? – Alessandro Teruzzi Nov 15 '10 at 11:46

4 Answers4

5

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 :)

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • 1
    Liked this solution. Posted the detailed code here: [C++ iterate into nested struct field with boost fusion adapt_struct](http://stackoverflow.com/q/12084781/362754) – minghua Sep 06 '12 at 07:10
3

I don't know of any way to do what you want directly, but you might want to take a look at clang, which is a compiler front-end implementation that you can make use of to do other things:

http://clang.llvm.org

I guess you'd then be able to traverse the abstract syntax tree it creates and get at the information you're after.

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
2

Well, standard C++ compilers can't do that, they lack reflection capabilities.

Sounds like a task for a code generator. So either use a toolkit to extract these informations from the headers or generate both headers and serialization functions from another source. Just make sure you do not repeat yourself.

bltxd
  • 8,825
  • 5
  • 30
  • 44
  • Code generation sure sounds like the best way here, that way you only have to write the `operator<<` functions once and existing headers do not have to change. – Steve Townsend Nov 12 '10 at 17:15
1

I am afraid but C++ doesn't support reflection. You can use Boost.TypeTraits to achieve a restricted form of reflection at compile time.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345