By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

check for polymorphism during runtime

P: n/a
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
Jun 26 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jan Schäfer wrote:
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/?m...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
Jun 26 '06 #2

P: n/a
Jan Schäfer wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.