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! ]
|
|
0
|
|
|
|
Reply
|
catphive (45)
|
2/26/2010 7:57:22 AM |
|
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! ]
|
|
0
|
|
|
|
Reply
|
Seungbeom
|
2/26/2010 11:47:27 AM
|
|
On 26 Feb., 09:57, Brendan <catph...@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! ]
|
|
0
|
|
|
|
Reply
|
nabulke
|
2/26/2010 11:50:22 AM
|
|
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! ]
|
|
0
|
|
|
|
Reply
|
Francis
|
2/26/2010 9:30:51 PM
|
|
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! ]
|
|
0
|
|
|
|
Reply
|
Seungbeom
|
2/26/2010 9:31:51 PM
|
|
|
4 Replies
535 Views
(page loaded in 0.078 seconds)
Similiar Articles: exact difference between C style and static_cast, const_cast ...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 ... What's the difference? - comp.databases.btrieveexact difference between C style and static_cast, const_cast ... O wise and powerful clcm, What's the precise difference between C style casts, and static_cast. multiple inheritance usage - comp.soft-sys.matlabexact difference between C style and static_cast, const_cast ... multiple inheritance usage - comp.soft-sys.matlab exact difference between C style and static_cast, const ... C style question - comp.lang.cexact difference between C style and static_cast, const_cast ..... com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast http://stackoverflow.com/questions ... What is Difference between const int i and int const i - comp.lang ...exact difference between C style and static_cast, const_cast ..... clcm, What's the precise difference between C style casts, and static_cast. Static const integral data members can be initialized? - comp.lang ...exact difference between C style and static_cast, const_cast ... Static const integral data members can be initialized? - comp.lang ... > The difference is that floating ... "Moderated" List??? - comp.lang.asm.x86exact difference between C style and static_cast, const_cast ... O wise and powerful clcm, What's the precise difference between C style casts, and static_cast. Why no std::back_insert_iterator::value_type? - comp.lang.c++ ...... C-style cast (value_type)(val) so it could apply either a static_cast or > > a reinterpret_cast (plus an additional const_cast). ... static_cast<type>(x)? The differences ... What is the max length for a string sent through WM_COPYDATA ...... cd.cbData = static_cast<DWORD>(mytext.size()) + 1; > cd.lpData = const_cast<char *>(mytext.c_str ... thats just a matter of style ... Another difference is, that the ... Misuses of RTTI - comp.lang.c++.moderated... obj) == typeid(foo)) { foo* foo = static_cast<foo ... or other mechanisms to avoid losing track of the exact ... to files I have to use self written code using C-style ... exact difference between C style and static_cast, const_cast C++/VBC++/VB - exact difference between C style and static_cast, const_cast exact difference between C style and static_cast, const_cast ...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 ... 7/23/2012 3:29:05 PM
|