Casting

J

Jakob Bieling

Hi,

I remember having read (either here or in c.l.c++.m) that the
C++-style casts cover almost all casts you could do with a C-style cast,
except forone more or less rare case. But whereever I look, there is no
mention of this one case .. actually, I found people saying that *any*
C-style cast can be written as a combination of C++-style casts.

Could someone shed some light on this please?

thanks!
 
G

Gavin Deane

Jakob said:
Hi,

I remember having read (either here or in c.l.c++.m) that the
C++-style casts cover almost all casts you could do with a C-style cast,
except forone more or less rare case. But whereever I look, there is no
mention of this one case .. actually, I found people saying that *any*
C-style cast can be written as a combination of C++-style casts.

Could someone shed some light on this please?

A C-style cast will let you convert to an inaccessible base class.

Rather than explain what that means, see posts by Ron Natalie in this
thread (from which I copied that statement).

http://groups.google.co.uk/group/co...ccessible+base+class"&rnum=1#59052e73591ce2e6

Gavin Deane
 
A

Alf P. Steinbach

* Jakob Bieling:
I remember having read (either here or in c.l.c++.m) that the
C++-style casts cover almost all casts you could do with a C-style cast,
except forone more or less rare case. But whereever I look, there is no
mention of this one case .. actually, I found people saying that *any*
C-style cast can be written as a combination of C++-style casts.

Could someone shed some light on this please?

C-cast that can't be expressed as C++ cast:

(InaccessibleBase*) pObject

C++ cast that can't be expressed as C cast:

dynamic_cast<whatever>

Hth.,

- Alf
 
J

Jack Klein

Hi,

I remember having read (either here or in c.l.c++.m) that the
C++-style casts cover almost all casts you could do with a C-style cast,
except forone more or less rare case. But whereever I look, there is no
mention of this one case .. actually, I found people saying that *any*
C-style cast can be written as a combination of C++-style casts.

Could someone shed some light on this please?

thanks!

There is no C++ cast to convert between a pointer to function and a
pointer to object (including void *). Every C and C++ compiler that I
know of allows this with a C cast:

int func(int x)
{
return x + 1;
}

int main()
{
void *vp = (void *)func;
/* ... */
}

Of course the result is completely undefined, and I am fairly sure
that there is nothing in the C standard that requires a compiler to
accept it, that is aside from the fact that a compiler is never
obliged to accept source containing guaranteed undefined behavior.

But several platforms, including POSIX and Windows, depend on this
behavior and doing "what they expect" when the pointer sizes are the
same.
 
T

Tomás

There is no C++ cast to convert between a pointer to function and a
pointer to object (including void *).


reinterpret_cast ?

I haven't used it in a while, but I used to do all sorts of mad stuff
with it back in the day.

-Tomás
 
J

Jakob Bieling

Tomás said:
reinterpret_cast ?

I haven't used it in a while, but I used to do all sorts of mad stuff
with it back in the day.

According to the Standard, reinterpret_cast can only convert
function pointers to function pointers of different type (converting
function pointer to void* is not listed).

Unfortunately it only lists things you *can* do. Anything not
listed, is not valid. This is what made it difficult for me to find that
particular case I was asking for in my first post.

hth
 
R

red floyd

Jakob said:
According to the Standard, reinterpret_cast can only convert
function pointers to function pointers of different type (converting
function pointer to void* is not listed).

Unfortunately it only lists things you *can* do. Anything not
listed, is not valid. This is what made it difficult for me to find that
particular case I was asking for in my first post.

hth

Nope, you've got to use a C-style cast. Document the hell out of it,
and get managerial approval for such a nasty practice. I've had to do
it that way when trying to print the value of a function pointer -- use
a C-Style cast to cast void (*)(void*) to void* so that I could pass it
to an ostream for display. Pete Becker and I had a thread going about
it here a while back.
 
J

Jakob Bieling

red floyd said:
Jakob Bieling wrote:
There is no C++ cast to convert between a pointer to function and a
pointer to object (including void *).
reinterpret_cast ?
According to the Standard, [..] converting
function pointer to void* is not listed [..]
Unfortunately it only lists things you *can* do. Anything not
listed, is not valid.
Nope, you've got to use a C-style cast.

Looks like we are agreeing ;) Should have made the point more
obvious.

regards
 
T

Tomás

red floyd posted:
Nope, you've got to use a C-style cast. Document the hell out of it,
and get managerial approval for such a nasty practice. I've had to do
it that way when trying to print the value of a function pointer -- use
a C-Style cast to cast void (*)(void*) to void* so that I could pass it
to an ostream for display. Pete Becker and I had a thread going about
it here a while back.


template<typename T, typename S>
T hardcore_cast(S s)
{
return (T)s;
}

Or something like that.

functionpointer_cast
pointer_cast


-Tomás
 

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,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top