Object introspection

P

pradeepta

Is there a mechanism in C++ or any library which will give typeinfo
about the members of a struct/class. Something like Boost::type_traits
which will expose type_info of the members too.
 
M

mlimber

Is there a mechanism in C++ or any library which will give typeinfo
about the members of a struct/class. Something like Boost::type_traits
which will expose type_info of the members too.

Only if you expose the typeinfo of the members by making the data
members accessible (i.e., public for everyone, protected for
subclasses, or open to friendships) or by publishing one or more member
functions to make it available to the user of the class. Generally
speaking, exposing such information would violate the principle of
encapsulation, which rightly tries to hide implementation details (cf.
http://www.parashift.com/c++-faq-lite/classes-and-objects.html#faq-7.4).

Cheers! --M
 
P

pradeepta

Let me explain furthur, I want to write a smart structure copy template
function which will inspect the argument (struct/class template) and do
a member-wise assignment across the two arguments.

<class T> const T& smart_copy(const T& arg1, T& arg2)

using template metaprogramming (Eg using BOOST_PP* like functions)
 
R

red floyd

Let me explain furthur, I want to write a smart structure copy template
function which will inspect the argument (struct/class template) and do
a member-wise assignment across the two arguments.

<class T> const T& smart_copy(const T& arg1, T& arg2)

using template metaprogramming (Eg using BOOST_PP* like functions)

What's wrong with using T's operator=(), which knows how to do it properly?

e.g.

template<class T>
const T& smart_copy(const T& arg1, T& arg2)
{
return arg2 = arg1;
}
 
P

pradeepta

I am trying to use templating to do lot of boilerplate implementations
from an existing codebase (in C) using a c++ compiler (gcc). One way
would be for me to write a correct "=" operation for each struct/class
and use it, but then it is tedious and error-prone (and I am a lazy
person).
Secondly I would like to write generic functions which can introspect
the template parameters and do "stuff" with them. If boost::type_traits
goes so far as to give me some properties of my type, why not give me
the names/types etc of members of that type?
Just curious...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top