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.)
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.
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.
QT has a primitive form of reflection, you probably want to take a loot at it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With