From: Brendan on
O wise and powerful clcm,

What's the precise difference between C style casts, and static_cast.
I've always been told that static_cast and const_cast casts are
better, but I'd like to know exactly in what circumstances they differ
so I can make that judgement myself.

Giving me a reference to something in the C++ standard is fine...

Thanks,
Brendan

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Seungbeom Kim on
Brendan wrote:
> O wise and powerful clcm,
>
> What's the precise difference between C style casts, and static_cast.
> I've always been told that static_cast and const_cast casts are
> better, but I'd like to know exactly in what circumstances they differ
> so I can make that judgement myself.
>
> Giving me a reference to something in the C++ standard is fine...

ISO/IEC 14882:2003, 5.4/5:

The conversions performed by
� a const_cast (5.2.11),
� a static_cast (5.2.9),
� a static_cast followed by a const_cast,
� a reinterpret_cast (5.2.10), or
� a reinterpret_cast followed by a const_cast,
can be performed using the cast notation of explicit type conversion.
[...] If a conversion can be interpreted in more than one of the ways
listed above, the interpretation that appears first in the list is used,
even if a cast resulting from that interpretation is ill-formed.
If a conversion can be interpreted in more than one way as a static_cast
followed by a const_cast, the conversion is ill-formed.
--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: nabulke on
On 26 Feb., 09:57, Brendan <catph...(a)catphive.net> wrote:

> What's the precise difference between C style casts, and static_cast.
> I've always been told that static_cast and const_cast casts are
> better, but I'd like to know exactly in what circumstances they differ
> so I can make that judgement myself.
[excess quoting deleted -mod]
Please check this:

http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast
http://stackoverflow.com/questions/1609163/what-is-the-difference-between-static-cast-and-c-style-casting
http://stackoverflow.com/questions/32168/c-cast-syntax-styles

Hope this helps
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
Seungbeom Kim wrote:
> Brendan wrote:
>> O wise and powerful clcm,
>>
>> What's the precise difference between C style casts, and static_cast.
>> I've always been told that static_cast and const_cast casts are
>> better, but I'd like to know exactly in what circumstances they differ
>> so I can make that judgement myself.
>>
>> Giving me a reference to something in the C++ standard is fine...
>
> ISO/IEC 14882:2003, 5.4/5:
>
> The conversions performed by
> � a const_cast (5.2.11),
> � a static_cast (5.2.9),
> � a static_cast followed by a const_cast,
> � a reinterpret_cast (5.2.10), or
> � a reinterpret_cast followed by a const_cast,
> can be performed using the cast notation of explicit type conversion.
> [...] If a conversion can be interpreted in more than one of the ways
> listed above, the interpretation that appears first in the list is used,
> even if a cast resulting from that interpretation is ill-formed.
> If a conversion can be interpreted in more than one way as a static_cast
> followed by a const_cast, the conversion is ill-formed.
> --
> Seungbeom Kim

And a major difference is that you cannot remove a const (or volatile)
qualification by using a static_cast. This gives added protection and
allows us to write const-correct code which the compiler can check for us.

Another subtle difference is when casting pointers. A static-cast can
modify the pointer in ways that a C-style cast will not. This can be
significant when dealing with multiple inheritance.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Seungbeom Kim on
Seungbeom Kim wrote:
> Brendan wrote:
>> O wise and powerful clcm,
>>
>> What's the precise difference between C style casts, and static_cast.
>> I've always been told that static_cast and const_cast casts are
>> better, but I'd like to know exactly in what circumstances they differ
>> so I can make that judgement myself.
>>
>> Giving me a reference to something in the C++ standard is fine...
>
> ISO/IEC 14882:2003, 5.4/5:
>
> The conversions performed by
> � a const_cast (5.2.11),
> � a static_cast (5.2.9),
> � a static_cast followed by a const_cast,
> � a reinterpret_cast (5.2.10), or
> � a reinterpret_cast followed by a const_cast,
> can be performed using the cast notation of explicit type conversion.
> [...] If a conversion can be interpreted in more than one of the ways
> listed above, the interpretation that appears first in the list is used,
> even if a cast resulting from that interpretation is ill-formed.
> If a conversion can be interpreted in more than one way as a static_cast
> followed by a const_cast, the conversion is ill-formed.

I forgot to add this, which may be relevant:

5.4/7:

In addition to those conversions, the following static_cast and
reinterpret_cast operations (optionally followed by a const_cast
operation) may be performed using the cast notation of explicit type
conversion, even if the base class type is not accessible:

� a pointer to an object of derived class type or an lvalue of derived
class type may be explicitly converted to a pointer or reference to
an unambiguous base class type, respectively;

� a pointer to member of derived class type may be explicitly converted
to a pointer to member of an unambiguous non-virtual base class type;

� a pointer to an object of non-virtual base class type, an lvalue of
non-virtual base class type, or a pointer to member of non-virtual base
class type may be explicitly converted to a pointer, a reference, or
a pointer to member of a derived class type, respectively.

This states that the C-style cast can do something that C++-style casts
cannot do.

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]