: Does anyone know of a good way to use reflection in c++?
:
: I don't mean simply using rtti or dynamic casting.
:
: I'm talking about java/c# style reflection where an actual instance of
: an object can be constructed through reflection and method calls can
be
: made via the reflection apis.
As others pointed out, there is no support for reflection
in the C++ standard.
But among the papers that were submitted for consideration
in C++0x, one was quite interestingly addressing this problem:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1471.pdf
Idea: instead of storing lots of run-time information, allow
compile-time code to process type info, and generate the desired
runtime information...
See also, for uses of reflection:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1775.pdf
Unfortunately, this will not be for C++09. See the status of these
papers in:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2389.html
"Not ready for C++0x, but open to resubmit in future"
Food for fantasy...
This said, today, template functions can help easily implement
basic tools like factory functions. Other smart tricks also
help reduce the effort required to implement serialization
(e.g. see the archive interface in boost::serialization,
allowing a single template function to implement both
input and output, avoiding a common duplication).
Regards,
Ivan