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

Casting

P: n/a
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!
--
jb

(reply address in rot13, unscramble first)
Feb 14 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a

Jakob Bieling wrote:
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/com...052e73591ce2e6

Gavin Deane

Feb 14 '06 #2

P: n/a
* 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
--
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?
Feb 14 '06 #3

P: n/a
Thanks for the replies, this has been what I was looking for :)
--
jb

(reply address in rot13, unscramble first)
Feb 14 '06 #4

P: n/a
On Tue, 14 Feb 2006 10:31:20 +0100, "Jakob Bieling"
<ar****************@rot13.com> wrote in comp.lang.c++:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 15 '06 #5

P: n/a
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
Feb 15 '06 #6

P: n/a
Tomás <NU**@NULL.NULL> wrote:
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.


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
--
jb

(reply address in rot13, unscramble first)
Feb 15 '06 #7

P: n/a
Jakob Bieling wrote:
Tomás <NU**@NULL.NULL> wrote:
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.


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.
Feb 15 '06 #8

P: n/a
red floyd <no*****@here.dude> wrote:
Jakob Bieling wrote:
Tomás <NU**@NULL.NULL> 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
--
jb

(reply address in rot13, unscramble first)
Feb 15 '06 #9

P: n/a
red floyd posted:
Jakob Bieling wrote:
Tomás <NU**@NULL.NULL> wrote:
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.


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.

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

Or something like that.

functionpointer_cast
pointer_cast
-Tomás
Feb 15 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.