check for polymorphism during runtime

  • Thread starter =?iso-8859-1?q?Jan_Sch=E4fer?=
  • Start date
?

=?iso-8859-1?q?Jan_Sch=E4fer?=

Hi all,

is there a simple way to find out during runtime if a class of some type
is polymorphic? I am writing some serialization functions and need to
handle polymorphic and non-polymorphic classes differently. I am sure
there are hundreds of workarounds for this, but I am interested if this
can be done by some clever function with the help of casting and/or rtti
which determines if a class is polymorphic or not.
I found a is_polymorphic template in the boost libraries but didn't get
the trick yet!?

Any ideas?

Greetings from Ulm

Jan
 
T

Tom Widmer

Jan said:
Hi all,

is there a simple way to find out during runtime if a class of some type
is polymorphic? I am writing some serialization functions and need to
handle polymorphic and non-polymorphic classes differently. I am sure
there are hundreds of workarounds for this, but I am interested if this
can be done by some clever function with the help of casting and/or rtti
which determines if a class is polymorphic or not.
I found a is_polymorphic template in the boost libraries but didn't get
the trick yet!?

Any ideas?

You've answered your own question - boost::is_polymorphic (a.k.a
std::tr1::is_polymorphic). It's part of the new extension to the
standard, TR1, and an implementation is also available from here:
http://www.dinkumware.com/manuals/?manual=compleat&page=typetrait.html#is_polymorphic

There is no portable standard conforming solution AFAIK - boost relies
on a non-portable hack to do with the fact that polymorphic classes have
a vtable pointer, and the standard library version can do it how it likes.

IIRC the boost implementation is based on this non-portable idea:

template <class T>
struct is_polymorphic
{
private:
struct dummy: T
{
virtual void f();
};

public:
static bool const value = sizeof(dummy) == sizeof(T);
};

If adding a virtual function doesn't change the size of the class, it
must already have a vtable pointer. Obviously, a real implementation
will handle non-class types too (returning false).

Tom
 
V

Victor Bazarov

Jan said:
is there a simple way to find out during runtime if a class of some
type is polymorphic?

You mean, if any of its members are declared 'virtual'? Not portably.
I am writing some serialization functions and
need to handle polymorphic and non-polymorphic classes differently. I
am sure there are hundreds of workarounds for this, but I am
interested if this can be done by some clever function with the help
of casting and/or rtti which determines if a class is polymorphic or
not.
I found a is_polymorphic template in the boost libraries but didn't
get the trick yet!?

So, why don't you just use Boost, then?
Any ideas?

Use the work-arounds that you know of.

V
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top