473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

is_polymorphic

sks
Hello.

I saw this on the message boards but I don't see any explanation on how/why
this works. Doesn't typeid operator only take 1 argument?
template<class T> bool is_polymorphic( const T&r )
{
bool result = false;
typeid(result = true, r);
return result;
}
Thanks.


Dec 3 '05 #1
9 2003
sks wrote:
I saw this on the message boards but I don't see any explanation on
how/why this works. Doesn't typeid operator only take 1 argument?
template<class T> bool is_polymorphic( const T&r )
{
bool result = false;
typeid(result = true, r);
return result;
}


Yes, typeid has only one operand. Are you forgetting the comma
operator?

V
Dec 3 '05 #2
sks

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:8I******************************@comcast.com. ..
sks wrote:
I saw this on the message boards but I don't see any explanation on
how/why this works. Doesn't typeid operator only take 1 argument?
template<class T> bool is_polymorphic( const T&r )
{
bool result = false;
typeid(result = true, r);
return result;
}


Yes, typeid has only one operand. Are you forgetting the comma
operator?

V


So there is a comma operator that is overloaded for type T or is there some
default/global comma operator? What exactly is the functionality of a comma
operator?

Dec 3 '05 #3
* sk*@yahoo.com:

I saw this on the message boards but I don't see any explanation on how/why
this works. Doesn't typeid operator only take 1 argument?
template<class T> bool is_polymorphic( const T&r )
{
bool result = false;
typeid(result = true, r);
return result;
}


First, syntax: typeid is an operator, not a function. So the
parenthesis isn't the parenthesis of a function call's argument list.
It's simply a parenthesis, around a comma expression, which is the
single argument to typeid.

The comma operator, when executed, evaluates each argument in sequence
and the result of the expression is the result of the last argument.

Now, semantics. When the expression that is the single argument to
typeid has non-polymorphic static type it isn't evaluated. So in that
case the 'result = true' part isn't executed. But when the expression
is of polymorphic type it is evaluated.

For default-constructible classes an in-practice way of doing this
determination at compile time is

template<class T>
class IsPolymorphic
{
private:
class Derived: T { virtual ~Derived() {} };
public:
enum{ yes = (sizeof(Derived)==sizeof(T)), no = !yes };
};

but this is not guaranteed to work on all compilers, it's only
in-practice, since it relies on (1) having a vtable pointer added when
there are virtual functions, and (2) no size overhead for deriving a
class with no data members, neither of which is guaranteed.

I've never needed either construction, but perhaps determining whether a
class is polymorphic or not, can be useful for something...

Do you recall what it was used for?

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 3 '05 #4
sks wrote:
....

So there is a comma operator that is overloaded for type T or is there some
default/global comma operator? What exactly is the functionality of a comma
operator?


In this case it's using the usual "," operator.

expr : expr , expr

The expressions are evaluated left to right and the result and type of
the expression is the value of the last expression.

It's most commonly used in for loops - like this.

for ( i=1, j=2; i < j; ++ i )
Dec 3 '05 #5

Alf P. Steinbach wrote:
I've never needed either construction, but perhaps determining whether a
class is polymorphic or not, can be useful for something...

Do you recall what it was used for?


I can think of only point - dynamic_cast is possible only for a
polymorphic type.
(But doing a dynamic_cast on a non-polymorphic type anyways flags an
error at compile time, so that should not be a major usage)

Also, it can be possible to decide whether it is "legal" to derive from
a class or not, without actually looking at the entire interface of the
base class.
(However, this also doesnot sound a very good usage)

I believe there is something more to it. Could you please explain Alf?

Dec 3 '05 #6
* Neelesh Bodas:

Alf P. Steinbach wrote:
I've never needed either construction, but perhaps determining whether a
class is polymorphic or not, can be useful for something...

Do you recall what it was used for?


I can think of only point - dynamic_cast is possible only for a
polymorphic type.
(But doing a dynamic_cast on a non-polymorphic type anyways flags an
error at compile time, so that should not be a major usage)

Also, it can be possible to decide whether it is "legal" to derive from
a class or not, without actually looking at the entire interface of the
base class.
(However, this also doesnot sound a very good usage)

I believe there is something more to it. Could you please explain Alf?


I'm as baffled as you. It's possible that one could refuse to serialize
to binary form a polymorphic object*. But that would anyway be an
in-practice thing, because non-polymorphic is not the same as POD.

*) I got that idea from checking references to this in the Boost
library, which btw. has a non-restricted in-practice compile time
is_polymorphic, same idea as the one I showed but simply not
implementing the constructor, just declaring it.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 3 '05 #7

"Neelesh Bodas" <ne***********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

Alf P. Steinbach wrote:
I've never needed either construction, but perhaps determining whether a
class is polymorphic or not, can be useful for something...

Do you recall what it was used for?


I can think of only point - dynamic_cast is possible only for a
polymorphic type.
(But doing a dynamic_cast on a non-polymorphic type anyways flags an
error at compile time, so that should not be a major usage)

Also, it can be possible to decide whether it is "legal" to derive from
a class or not, without actually looking at the entire interface of the
base class.
(However, this also doesnot sound a very good usage)


That's certainly an interesting idea, but IMHO it might be good practice to
think about this problem at "implementation-time" and not at runtime using a
function query ;-)

Cheers
Chris
Dec 3 '05 #8

"Alf P. Steinbach" <al***@start.no> wrote in message
news:43*****************@news.individual.net...
[SNIP]

*) I got that idea from checking references to this in the Boost
library, which btw. has a non-restricted in-practice compile time
is_polymorphic, same idea as the one I showed but simply not
implementing the constructor, just declaring it.


Could you please elaborate why/how this works with the declared but not
implemented ctor, because I'm a little lost at the moment.

Cheers
Chris
Dec 3 '05 #9
* Chris Theis:

"Alf P. Steinbach" <al***@start.no> wrote in message
news:43*****************@news.individual.net...
[SNIP]

*) I got that idea from checking references to this in the Boost
library, which btw. has a non-restricted in-practice compile time
is_polymorphic, same idea as the one I showed but simply not
implementing the constructor, just declaring it.


Could you please elaborate why/how this works with the declared but not
implemented ctor, because I'm a little lost at the moment.


Consider:

#include <iostream>
#include <ostream>

struct NotDefaultConstructible
{
NotDefaultConstructible( double ) {}
};

struct Derived: NotDefaultConstructible
{
Derived();
virtual ~Derived() {}
};

int main()
{
std::cout << sizeof( Derived ) << std::endl;
}

If you comment out the Derived default constructor declaration then the
compiler must generate a default constructor, which it can't do.

Derived can not be instantiated but that doesn't matter.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 3 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vianney Lançon | last post by:
J'ai eu recemmet à implémenter des fonctions du type is_pointer<> is_polymorphic<> if_then_else<> qui donnent leur résultat à la compilation. Un peu comme dans la bibliotheque mpl de boost. On...
5
by: News Admin | last post by:
I have a bunch of classes, instances of which that need to live in shared memory and be accessed by multiple processes. This means that they cannot have a vtable as the addresses in it will be in...
5
by: Kai-Uwe Bux | last post by:
Hi, I decided to abandon the direct use of raw pointers in my programs and intend to replace them with templates like: dyn_array_of < typename T, typename Allocator > pointer_to < typename...
18
by: Tarundeep | last post by:
hi, let us say it is a 32 bit processor then the size of the pointer is 32 bits. now i want to know what would be the size of the class with vtable pointer in it , that is it has few virtual...
20
by: verec | last post by:
One problem I've come accross in designing a specific version of auto_ptr is that I have to disntiguish between "polymorphic" arguments and "plain" ones, because the template has to, internally,...
6
by: ma740988 | last post by:
I was trying to garner a feel for typeid and it's use with polymorphic types #include <iostream> template <class T> bool is_polymorphic() { bool result(false); typeid( (result=true),...
2
by: Jan Schäfer | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.