• Advertisement

Archived

This topic is now archived and is closed to further replies.

C++ Introspection

This topic is 5129 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright I have to admit that I have been just learning some of the finer points to C++ -- casting up and down, namespaces, etc. What I''m thinking of here is how to do some kind of introspection -- I''m not really seeing how this is done, or can be done. Although just in passing, I think I saw something in the STL about introspection. Am I wrong? Does anyone know how this is done? Thanks, L-

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
C++''s base language types are not introspective. For instance, you cannot write a routine that will serialize any class, because each class doesn''t know anything about itself other than what you tell it.

(Okay, you could fwrite entire classes to files but this is stupid and dumb.)

Share this post


Link to post
Share on other sites
yeah, i realize the base language is totally non-introspective (capable of telling you what it is.) However I think QT and some of the STL have introspection methods -- I''m just trying to get an idea of how they accomplished it.

thanks,
L-

Share this post


Link to post
Share on other sites
QT gets the benefit of being run through the metaobject compiler (MOC) before being passed onto the actual compiler, at which point I think type information can be generated and inserted into the final file. I''m not sure on how it works, I know MOC is required for signals/slots.

As for STL, how can it be introspective? Everything it does is through templates.

Share this post


Link to post
Share on other sites
quote:
Original post by brain21
What is introspection?


Broadly speaking, the ability to query a construct for its supported features, at run time.

e.g. - Getting an object to provide you with a list of its member functions, and being able to use that list to actually call them.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share this post


Link to post
Share on other sites
You have to generate the classes using an automatic means (meta-template), or use macro wrappers around your declarations which also add stuff to the class for introspection.

As an exercise, I wrote a meta-template that would print out the class declaration from such a class description.

As useful as this capabilities seems, I haven''t found it all that useful in practice. Too often you can''t serialize the class automatically, something special needs to happen, and I never wanted to expose every method of a class to the run-time shell.

-- It was more work segregating conceptual objects into multiple classes to utilize introspection automation, than it was to just code it up for each class.

Share this post


Link to post
Share on other sites

  • Advertisement