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

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

    Reflection is essentially about what the compiler decided to leave as footprints in the code that the runtime code can query. C++ is famous for not paying for what you don't use; because most people don't use/want reflection, the C++ compiler avoids the cost by not recording anything.

    So, C++ doesn't provide reflection, and it isn't easy to "simulate" it yourself as general rule as other answers have noted.

    Under "other techniques", if you don't have a language with reflection, get a tool that can extract the information you want at compile time.

    Our DMS Software Reengineering Toolkit is generalized compiler technology parameterized by explicit langauge definitions. It has langauge definitions for C, C++, Java, COBOL, PHP, ...

    For C, C++, Java and COBOL versions, it provides complete access to parse trees, and symbol table information. That symbol table information includes the kind of data you are likely to want from "reflection". If you goal is to enumerate some set of fields or methods and do something with them, DMS can be used to transform the code according to what you find in the symbol tables in arbitrary ways.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题