How can I add reflection to a C++ application?

前端 未结 28 1653
感动是毒
感动是毒 2020-11-21 11:25

I\'d like to be able to introspect a C++ class for its name, contents (i.e. members and their types) etc. I\'m talking native C++ here, not managed C++, which has reflection

相关标签:
28条回答
  • 2020-11-21 11:58

    This question is a bit old now (don't know why I keep hitting old questions today) but I was thinking about BOOST_FUSION_ADAPT_STRUCT which introduces compile-time reflection.

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

    I really think a macro to encapsulate the BOOST_FUSION_ADAPT_STRUCT one could generate the necessary methods to get the runtime behavior.

    0 讨论(0)
  • 2020-11-21 11:58

    Ponder is a C++ reflection library, in answer to this question. I considered the options and decided to make my own since I couldn't find one that ticked all my boxes.

    Although there are great answers to this question, I don't want to use tonnes of macros, or rely on Boost. Boost is a great library, but there are lots of small bespoke C++0x projects out that are simpler and have faster compile times. There are also advantages to being able to decorate a class externally, like wrapping a C++ library that doesn't (yet?) support C++11. It is fork of CAMP, using C++11, that no longer requires Boost.

    0 讨论(0)
  • 2020-11-21 11:59

    Check out Classdesc http://classdesc.sf.net. It provides reflection in the form of class "descriptors", works with any standard C++ compiler (yes it is known to work with Visual Studio as well as GCC), and does not require source code annotation (although some pragmas exist to handle tricky situations). It has been in development for more than a decade, and used in a number of industrial scale projects.

    0 讨论(0)
  • 2020-11-21 12:01

    EDIT: Updated broken link as of February, the 7th, 2017.

    I think noone mentioned this:

    At CERN they use a full reflection system for C++:

    CERN Reflex. It seems to work very well.

    0 讨论(0)
提交回复
热议问题