17

I'm currently going to write big project in c++11.

I'm searching for some time good c++11/c++ reflection library and I've found several different libraries, but most of them are simply not updated for last couple of years or their functionality is very limited.

Could you tell me if there is a really good library for c++1/c++ for reflection? (I want to have static and dynamic reflection, know as much information as I can about methods, classes etc, can dynamically add and access methods etc.)

Or maybe c++11 has provided some additional functionality that will help to better design reflection libraries and should I write it by myself? (I haven't found information about it though.)

Xeo
  • 129,499
  • 52
  • 291
  • 397
Wojciech Danilo
  • 11,573
  • 17
  • 66
  • 132
  • 8
    It looks like your design doesn't match your choice of a language. – Yakov Galka Nov 20 '10 at 16:43
  • This is [a similar question]( http://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application). I was also looking and found [CAMP](https://github.com/tegesoft/camp). I have a [fork](https://github.com/billyquith/camp) going that removes the Boost dependency and uses C++11 instead. – Nick Dec 05 '15 at 11:35
  • I'm not sure how much this has changed over the years, but you may be able to accomplish some of the features of reflection through careful design. For example: the decorator pattern to add/modify behavior at CT or RT (https://en.wikipedia.org/wiki/Decorator_pattern); SFINAE to detect a function at CT https://stackoverflow.com/a/29319078/1043529; dep. inversion (https://en.wikipedia.org/wiki/Dependency_inversion_principle) to look for specific methods (CRTP and static_cast for CT, dynamic_cast for RT) and more. Full reflection for truly arbitrary types? Choose your battles, this one's uphill. – John P Dec 01 '17 at 14:26

4 Answers4

9

C++ is not really the best language for reflection. C++0x doesn't really change that. You can get limited support for static reflection using type traits, and you can even use SFINAE to statically determine whether a certain class has a particular member function or member variable. But that's really it.

Dynamic reflection is severely limited. You can get the type of a class at runtime using the <typeinfo> facilities, but that's about it.

As for static reflection, the ability to generically iterate over a class and get each member variable/function is just not possible without serious compromises. Boost.Fusion manages to pull this off by providing macros which allow you to bind an object to a tuple-like container. In fact, the std::tuple or boost::tuple class naturally provide compile-time reflection - in other words, you can statically iterate over a tuple and determine the type of each member. This gives you something approximating compile-time reflection over arbitrary aggregate types. Unfortunately, it's not as convenient as if there were native reflection support built in for arbitrary classes.

Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
5

It seems that there is a library that satisifies your "broad" requirements. Take a look at Mirror: Boost.Mirror. Note that it is officially NOT part of boost. You also could take a look at other libraries: Reflective Programming.

Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
3

Well depending on what you're after, you could build your own ontop of python and clang python bindings. There are a few examples out there, such as my own https://github.com/nevion/metapod - others out there exist if you scavenge hard enough and in a way this is what Qt's MOC tool is doing - except clang makes the task so much easier. One of the cool things about this approach is it works for all compilers because you're just generating standardized code with mako templates - but this also means it is not fully automatic - so there's a trade-off and it wont deal with every metaprogramming/reflection need. As Charles Salvia said, C++ isn't the best language for reflection so you have to take what you can get.

Jason Newton
  • 1,201
  • 9
  • 13
3

QT has a primitive form of reflection, you probably want to take a loot at it.

Gustavo Muenz
  • 9,278
  • 7
  • 40
  • 42