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
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
Edit 11/07/2019
The reflection TS is feature complete and is out for comment and vote over the summer (2019).
The meta-template programing approach is to be replaced with a simplier compile time code approach (not reflected in the TS).
Edit 10/02/2020
There is a request to support the reflection TS in Visual Studio here:
Talk on the TS by the author David Sankel:
http://cppnow.org/history/2019/talks/
https://www.youtube.com/watch?v=VMuML6vLSus&feature=youtu.be
Edit 17 March 2020
Progress on reflection is being made. A report from '2020-02 Prague ISO C++ Committee Trip Report' can be found here:
Details on what is being considered for C++23 can be found here (includes short section on Reflection):
Edit 4th June 2020
A new framework has been released by Jeff Preshing called 'Plywood' that contains a mechanism for runtime reflection. More details can be found here:
The tools and approach look to be the most polished and easiest to use so far.
Edit July 12 2020
Clang experiamental reflection fork : https://github.com/lock3/meta/wiki
Interesting reflection library that uses clang tooling library to extract informtion for simple reflection with no need to add macro's: https://github.com/chakaz/reflang