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. I realise C++ supplies some limited information using RTTI. Which additional libraries (or other techniques) could supply this information?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
There are two kinds of
reflection
swimming around.This is not possible with C++.
This kind of thing is possible with C++ using
template-tricks
. Useboost::type_traits
for many things (like checking whether a type is integral). For checking for the existance of a member function, use Is it possible to write a template to check for a function's existence? . For checking whether a certain nested type exists, use plain SFINAE .If you are rather looking for ways to accomplish 1), like looking how many methods a class has, or like getting the string representation of a class id, then i'm afraid there is no Standard C++ way of doing this. You have to use either
C++ is made with speed in mind. If you want high-level inspection, like C# or Java has, then I'm afraid i have to tell you there is no way without some effort.
You can find another library here: http://www.garret.ru/cppreflection/docs/reflect.html It supports 2 ways: getting type information from debug information and let programmer to provide this information.
I also interested in reflection for my project and found this library, i have not tried it yet, but tried other tools from this guy and i like how they work :-)
When I wanted reflection in C++ I read this article and improved upon what I saw there. Sorry, no can has. I don't own the result...but you can certainly get what I had and go from there.
I am currently researching, when I feel like it, methods to use inherit_linearly to make the definition of reflectable types much easier. I've gotten fairly far in it actually but I still have a ways to go. The changes in C++0x are very likely to be a lot of help in this area.
even though reflection is not supported out-of-the-box in c++, it is not too hard to implement. I've encountered this great article: http://replicaisland.blogspot.co.il/2010/11/building-reflective-object-system-in-c.html
the article explains in great detail how you can implement a pretty simple and rudimentary reflection system. granted its not the most wholesome solution, and there are rough edges left to be sorted out but for my needs it was sufficient.
the bottom line - reflection can pay off if done correctly, and it is completely feasible in c++.
What you need to do is have the preprocessor generate reflection data about the fields. This data can be stored as nested classes.
First, to make it easier and cleaner to write it in the preprocessor we will use typed expression. A typed expression is just an expression that puts the type in parenthesis. So instead of writing
int x
you will write(int) x
. Here are some handy macros to help with typed expressions:Next, we define a
REFLECTABLE
macro to generate the data about each field(plus the field itself). This macro will be called like this:So using Boost.PP we iterate over each argument and generate the data like this:
What this does is generate a constant
fields_n
that is number of reflectable fields in the class. Then it specializes thefield_data
for each field. It also friends thereflector
class, this is so it can access the fields even when they are private:Now to iterate over the fields we use the visitor pattern. We create an MPL range from 0 to the number of fields, and access the field data at that index. Then it passes the field data on to the user-provided visitor:
Now for the moment of truth we put it all together. Here is how we can define a
Person
class that is reflectable:Here is a generalized
print_fields
function using the reflection data to iterate over the fields:An example of using the
print_fields
with the reflectablePerson
class:Which outputs:
And voila, we have just implemented reflection in C++, in under 100 lines of code.
Reflection is not supported by C++ out of the box. This is sad because it makes defensive testing a pain.
There are several approaches to doing reflection:
The first link looks the most promising (uses mod's to clang), the second discusses a number of techniques, the third is a different approach using gcc:
http://www.donw.org/rfl/
https://bitbucket.org/dwilliamson/clreflect
https://root.cern.ch/how/how-use-reflex
There is now a working group for C++ reflection. See the news for C++14 @ CERN:
Edit 13/08/17: Since the original post there have been a number of potential advancements on the reflection. The following provides more detail and a discussion on the various techniques and status:
However it does not look promising on a standardised reflections approach in C++ in the near future unless there is a lot more interest from the community in support for reflection in C++.
The following details the current status based on feedback from the last C++ standards meeting:
Edit 13/12/2017
Reflection looks to be moving towards C++ 20 or more probably a TSR. Movement is however slow.
Edit 15/09/2018
A draft TS has been sent out to the national bodies for ballot.
The text can be found here: https://github.com/cplusplus/reflection-ts