8

Are there introspection techniques in C++ like those in python?

For example: I want to get more information about a specific object without going through the header file or referring back to cpp reference.

Am I asking a proper question, or moving the wrong direction here?

Update:

Based on the below answers, this answer is related to my question: How can I add reflection to a C++ application?

Community
  • 1
  • 1
securecurve
  • 5,589
  • 5
  • 45
  • 80
  • Are you referring to Reflection? https://www.google.com/search?q=reflection+in+c%2B%2B – Robert Harvey May 14 '13 at 17:43
  • I don't know about Reflection, may be it is .. I'm talking here from that point of view: being able to iterate over members of a type, enumerate its methods, attributes, and so on. – securecurve May 14 '13 at 17:47
  • 2
    Generally "no". The standard compilation model for C++ means that basically none of the type characteristics are retained in the compiled machine code, and thus they are not inspectable at runtime. – Kerrek SB May 14 '13 at 17:50
  • I guess this link: http://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application is very related to my question – securecurve May 14 '13 at 19:40

2 Answers2

4

C++ has a built in RTTI system, though it's for the most part horribly worthless. As a result custom introspection used instead.

Introspection in C++ is implemented with two main methods: preprocesing step where you scan cpp files and create a database/generate CPP code; use templating. I wrote some articles on the templating technique here.

If you're more interested in just using introspection rather than implementing it, I suggest looking up clReflect, or you can try cpfg.

Daniel Griscom
  • 1,834
  • 2
  • 26
  • 50
RandyGaul
  • 1,915
  • 14
  • 21
  • It seems to be less than straight forward as python does .. I need to consider the stuff you referred to above .. thanks! – securecurve May 14 '13 at 17:55
  • 3
    @securecurve Yes it's much more difficult since it is not supported by the language itself. – RandyGaul May 14 '13 at 17:55
  • Would it be more helpful to use an IDE that does that for me? As manual investigation of objects will be a headache more than a praise .... If you know one or more IDEs that supports intersection (whatever the underlying technique), please let me know ... thanks! – securecurve May 15 '13 at 00:10
  • You may have a look at Qt library with its QObject base class and its metaobject system. It provides discoverable properties and much more, and relies on "moc", a precompiler tool – Kiruahxh Apr 17 '19 at 06:19
4

Python and C++ are radically different languages. Normally, almost all of the type information is lost once you've finished compiling. About all you can do is ask if an object is a specific class (using typeid), or if it is an instance of a specific class (using dynamic_cast). In theory, you can also get the name of the type, using typeid::name(), but in practice, the standard doesn't impose anything useful for the return value, and some compilers (e.g. g++) don't give you anything useful.

James Kanze
  • 150,581
  • 18
  • 184
  • 329