473,402 Members | 2,061 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,402 software developers and data experts.

Converting pointer-to-member to int

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
Jul 19 '05 #1
7 5203
"cpp_weenie" <cp*@weenie.com> wrote in message
news:vo************@news.supernews.com...
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
Jul 19 '05 #2
"cpp_weenie" <cp*@weenie.com> wrote in message
news:vo************@news.supernews.com
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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 19 '05 #3
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:sN*****************@newsread4.news.pas.earthl ink.net

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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 19 '05 #4

"John Carson" <do***********@datafast.net.au> wrote in message
news:3f******@usenet.per.paradox.net.au...
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:sN*****************@newsread4.news.pas.earthl ink.net

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.


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
Jul 19 '05 #5
> '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.)

--
Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces


Jul 19 '05 #6
cpp_weenie wrote:
....

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


Why do you want to do this ?
Jul 19 '05 #7
"cpp_weenie" <cp*@weenie.com> wrote in message news:<vo************@news.supernews.com>...
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
Jul 19 '05 #8

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

Similar topics

1
by: gary | last post by:
I am interesting in converting all or at least any portions of a Access 2002 frontend.mdb and backend.mdb into VB.net. Are there any conversion tools for such a monstrous task? I am especially...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
20
by: joe | last post by:
Hi all! I just have quick, possibly stupid question.... is it possible to do the following: int func(){ int *pointer; foo(pointer); } int foo(int *pointer){
0
by: Mark Allen | last post by:
Hello, I am creating an RTF document server side for a report. However I am having problems converting images into the required RTF format. I am converting the image into a string (binary)...
6
by: Ramakrishnan Nagarajan | last post by:
Hi can any one help me in converting a DataView into a DataSet or a DataTable into a DataSet, I tried the following ways dSet= ((DataSet)_gridRegStudDetails.DataSource); dSet =...
0
by: StanD | last post by:
converting a project from vs2003 I am receiving an initial error for "_DateColumn" and "_Date" with my .xsd files referenced. What is the meaning of this. I am using SQLServer 2000. Thanks, ...
2
by: Alex Buell | last post by:
Is there an elegant way of converting strings containing digits between different number bases in C++? I.e.: 10 (base 2) = 2 (base 10) FF (base 16) = 256 (base 10) F (base 16) = 1111 (base 2)...
13
by: william | last post by:
code segment: long int * size; char entry; ............. size=&entry; ************************************* Gcc reported 'assignment of incompatible pointer type'.
3
by: zshenk | last post by:
I need help converting dates. Currently, I have several tables and databases that use the format 1/20/2007. I have a table right now that uses the 20070120 format, and I was hoping that someone...
34
by: sumedh..... | last post by:
double * X size of X->?? size of X->?? double (*X) size of X->?? size of X->??
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.