Converting pointer-to-member to int

C

cpp_weenie

Hello all,

Please consider this expression:

reinterpret_cast<int>(dmp)

Here, dmp is of type int Derived::*.

I have verified that sizeof(dmp) and sizeof(int) are the same.

The compiler balks with the following:

'reinterpret_cast' : cannot convert from 'int Derived::* ' to 'int'
There is no context in which this conversion is possible

Why would this happen?

Shouldn't a reinterpret_cast always succeed as long as the types involved
are the same size?

Thanks,
Dave
 
M

Mike Wahler

cpp_weenie said:
Hello all,

Please consider this expression:

reinterpret_cast<int>(dmp)

Here, dmp is of type int Derived::*.

Then your cast makes absolutely no sense.
What exactly are you trying to do.
I have verified that sizeof(dmp) and sizeof(int) are the same.

Sharing the same size with another does not endow a
either type with any conversion semantics.
The compiler balks with the following:

'reinterpret_cast' : cannot convert from 'int Derived::* ' to 'int'
There is no context in which this conversion is possible

Exactly true.
Why would this happen?

It's a language rule. What exactly are you trying to do?
Shouldn't a reinterpret_cast always succeed as long as the types involved
are the same size?

Not at all. reinterpret_cast is only for converting a base
class type to a derived class type (and it is not guaranteed to
always succeed). What C++ book(s) are you reading?

-Mike
 
J

John Carson

cpp_weenie said:
Hello all,

Please consider this expression:

reinterpret_cast<int>(dmp)

Here, dmp is of type int Derived::*.

I have verified that sizeof(dmp) and sizeof(int) are the same.

The compiler balks with the following:

'reinterpret_cast' : cannot convert from 'int Derived::* ' to 'int'
There is no context in which this conversion is possible

Why would this happen?

Shouldn't a reinterpret_cast always succeed as long as the types
involved are the same size?

No. reinterpret_cast is discussed in 5.2.10 of the Standard, which lists
conversions that should be possible. It certainly does *not* say that all
conversions between types of the same size are possible.

It says "A pointer can be explicitly converted to any integral type large
enough to hold it." but there is no such statement in relation to pointers
to members.
 
J

John Carson

Mike Wahler said:
Not at all. reinterpret_cast is only for converting a base
class type to a derived class type (and it is not guaranteed to
always succeed). What C++ book(s) are you reading?

Not true. reinterpret_cast has a much wider application. See 5.2.10 of the
Standard.
 
M

Mike Wahler

John Carson said:
Not true. reinterpret_cast has a much wider application. See 5.2.10 of the
Standard.

Wow, I surely have stuffed the old foot deep into my mouth
this time. I was thinking of 'dynamic_cast' (I have no idea
why :)) , which is a completely different animal.

Sorry about that. :)

-Mike
 
P

Phlip

'reinterpret_cast' : cannot convert from 'int Derived::* ' to 'int'
There is no context in which this conversion is possible

Why would this happen?

Shouldn't a reinterpret_cast always succeed as long as the types involved
are the same size?

Stroustrup invented member pointers without any legacy code to support, so
he took the liberty of denying us just one more way to shoot ourself in the
foot. No casts.

(Of course you can put the member pointer inside a structure, cast the
address of the structure into a pointer to an int, and dereference. Good
luck explaining the hole in your foot to the emergency room workers.)
 
S

Shane Beasley

cpp_weenie said:
reinterpret_cast<int>(dmp)

Here, dmp is of type int Derived::*.

I have verified that sizeof(dmp) and sizeof(int) are the same.

The compiler balks with the following:

'reinterpret_cast' : cannot convert from 'int Derived::* ' to 'int'
There is no context in which this conversion is possible

Why would this happen?

The conversion isn't defined by C++; hence, you can't do that.
Shouldn't a reinterpret_cast always succeed as long as the types involved
are the same size?

Yes and no; because there's no predefined conversion, reinterpret_cast
wouldn't know what to do. If you think you know better, you can try
this:

int i = reinterpret_cast<int &>(dmp);

That is, C++ treats dmp as an int and copies it as it would an int.
The behavior of this code is undefined by ISO, although the
implementation may define it. As a result, it's not portable; I'd
recommend against it.

Perhaps you can make do with offsetof in <cstddef> instead?

- Shane
 

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,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top