3

I've read some examples at Wikipedia but I'm looking for some real-life examples: how is introspection used, why (does it help to write clean code) and the code itself.

For example, is there a way to create a "generic" function to serialize any kind object? I mean: only one function in the parent, and all the descendent could be able to "save" + "restore" themselves into/from a file.

Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • "All is in the question" is not a good way to ask a question. Unless you're using only totally standard terms, you should explain your question a bit and illustrate the motivation, and indicate what you're trying to get out of this. – Kerrek SB Nov 15 '11 at 11:22

7 Answers7

1

There's RTTI in C++, and you can also use conditionals and dynamic_cast<>() to this effect, but in C++ we commonly strive to do as much as we can at compile time. If you feel a need for introspection chances are there are Better(tm) ways to achieve what you're aiming at with a static approach.

wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
  • If this doesn't sound convincing for you it must be because I'm vague. Can _you_ be specific, tell us what it is you're trying to achieve? – wilhelmtell Nov 15 '11 at 11:17
1

It isn't used (since it doesn't exist), there's no code examples (since it doesn't exist), and there's no reason to attempt to use it (since it doesn't exist).

The closest you can get is RTTI/dynamic_cast. But that's not really introspection.

Puppy
  • 144,682
  • 38
  • 256
  • 465
0

Not really. The closest you'll have is RTTI. But look at Qt metaobjects to see how they did it (also using their moc generator).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

I suggest you to look into boost::any

http://www.boost.org/doc/libs/1_47_0/doc/html/any.html

Sga
  • 3,608
  • 2
  • 36
  • 47
  • boost::any is a _one-size-fits-all_ type, but i don't see how it relates to introspection... – Adrien Plisson Nov 15 '11 at 11:23
  • since the only introspection in C++ is about types, I thought that boost::any it's a good example of it, and as the OP asked it's a real-life example, with code. – Sga Nov 15 '11 at 11:27
  • @AdrienPlisson I don't know who downvoted, but `boost::any` is a perfect example of how RTTI *introspection* (as described in the linked wikipedia article) is *used*. In particular, you can check the `any_cast` template. – David Rodríguez - dribeas Nov 15 '11 at 11:28
  • @Sga: If you start your comment with @ and the name, the user will be notified, which improves the chances of him reading your comment. Anyway, +1 – David Rodríguez - dribeas Nov 15 '11 at 11:30
  • @DavidRodríguez-dribeas : i did downvote. boost::any does not provide any builtin introspection capability into C++, it is just a "hack" which implements introspection. its like saying that C is a dynamic programming language, since you can use the python C library to execute some python code... – Adrien Plisson Nov 15 '11 at 11:34
  • @AdrienPlisson: The question asks about real life examples where `dynamic_cast` and/or `typeid` are/can be used to help write clean code. And this answer provides a reference to one such example: `boost::any` uses RTTI to help write type safe code in `any_cast`. How is this not an answer? – David Rodríguez - dribeas Nov 15 '11 at 11:42
0

For C++ there is RTTI if you must "go there"

More detailed example here on wikipedia

Extensive discussion on this topic already for reference

Community
  • 1
  • 1
David Wheaton
  • 608
  • 6
  • 11
0

From the linked article, it can be seen that you are referring to typeid and dynamic_cast<>. You should make this explicit in the question, as you will soon see that many people will jump straight without browsing through the links.

Now, how/when are they used? The simplest answer is that this should not generally be used. In most cases a well designed program does not need to perform runtime type checks, and if you find yourself doing it, then chances are that your design is in trouble.

Of course there are exceptions to all things, and in particular the example that @Sga gave is a really good one. In boost::any the library performs type-erasure to be able to hold any object inside the type, and then it uses RTTI manually in any_cast when extracting the value to verify that the type of the actual object is correct. Now, this is not common either, most programs don't use type-erasure and when they do, very few times they need to analyze the type at runtime.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • "In most cases a well designed program does not need to perform runtime type checks, and if you find yourself doing it, then chances are that your design is in trouble." => How could it be possible to serialize a class (create a "generic" serialize function)? Serializing is not about "bad design" I think. – Olivier Pons Nov 16 '11 at 10:13
  • @OlivierPons I did not say that it never makes sense, just that in most cases it doesn't. As of your particular example I have implemented serialisation a couple of times and have not needed manual RTTI checks, provide a serialise virtual function and call it on the object, then dynamic dispatch will make its magic. Note that you will need the serializable interface to be able to call the function in the first place... – David Rodríguez - dribeas Nov 16 '11 at 11:46
  • Yep but what you say implies that you still have to implement the "serialization function" on **each** child that want to be able to serialize itself. In Java, or Php, thanks to introspection, you just have to write this function *once* (and serialize/unserialize are part of php core functions actually => see http://php.net/manual/fr/function.serialize.php) – Olivier Pons Nov 16 '11 at 11:50
  • @olivierPons: yes, agreed, but you are talking about a whole different thing now: reflection must be supported by the language, and is much more powerful than manual RTTI access in c++. C++has no support for reflection. In the context of all languages, the sentence above would be: *in c++ runtime type checks are usually a code smell*. Again, just usually, there are a few examples where it makes sense (boost::any, for one) – David Rodríguez - dribeas Nov 16 '11 at 11:55
0

As others have said before, introspection is not a feature of C++ and the closest you can get to it is typeid() and dynamic_cast<>, but you don't really want to get that close.;)

Careful design of the system of ownerships, passing and containment of references, utilisation of a policy-based design can be used to get rid of this need for introspection: in a strictly OOP world objects aren't (supposed to be) required to know their own type (beyond providing virtual behavior, where defined so).

zyndor
  • 1,418
  • 3
  • 20
  • 36