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

前端 未结 28 1625
感动是毒
感动是毒 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:47

    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:

    1. use the debug information (non portable).
    2. Sprinkle your code with macro's/templates or some other source approach (looks ugly)
    3. Modify a compiler such as clang/gcc to produce a database.
    4. Use Qt moc approach
    5. Boost Reflect
    6. Precise and Flat 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:

    1. http://www.donw.org/rfl/

    2. https://bitbucket.org/dwilliamson/clreflect

    3. https://root.cern.ch/how/how-use-reflex

    There is now a working group for C++ reflection. See the news for C++14 @ CERN:

    • https://root.cern.ch/blog/status-reflection-c

    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:

    1. Static Reflection in a Nutshell
    2. Static Reflection
    3. A design for static reflection

    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:

    • Reflections on the reflection proposals

    Edit 13/12/2017

    Reflection looks to be moving towards C++ 20 or more probably a TSR. Movement is however slow.

    • Mirror
    • Mirror standard proposal
    • Mirror paper
    • Herb Sutter - meta programming including reflection

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

    • Draft TS as of 2019-06-17

    Edit 10/02/2020

    There is a request to support the reflection TS in Visual Studio here:

    • https://developercommunity.visualstudio.com/idea/826632/implement-the-c-reflection-ts.html

    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:

    • https://www.reddit.com/r/cpp/comments/f47x4o/202002_prague_iso_c_committee_trip_report_c20_is/

    Details on what is being considered for C++23 can be found here (includes short section on Reflection):

    • http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0592r4.html

    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:

    • https://preshing.com/20200526/a-new-cross-platform-open-source-cpp-framework/

    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

提交回复
热议问题