C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

static_cast vs. C style cast
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Vlad
Guest





PostPosted: Sat Mar 12, 2005 2:08 am    Post subject: static_cast vs. C style cast Reply with quote



Is there any real benefits of using static_cast instead of C-style cast
operation? It seems to me that it doesn't do much except of introducing
restriction on casting const expressions into non-const. (well it also
doesn't allow to cast to the virtual base class...)

Regards.


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





PostPosted: Sat Mar 12, 2005 11:34 am    Post subject: Re: static_cast vs. C style cast Reply with quote



Yes, there are many real benefits to using the C++-style casts than the
normal C style casts. The only thing is, the benefits (of most of the
casts) are decreased power in casting rather than increased, thus if
you wanted casts that allow more, wild, casts than the C-style casts,
you won't see the benefits in the C++ casts.

For example, using ordinary C-style casts, this block of code is legal

int *pi;
int i = (int)pi;

Whereas, using the static_cast, C++-style cast, it now becomes illegal,
more than likely erroring out with a message stating that a
reinterpret_cast is needed to make it legal.

The C++ casts allow for much safer casting than ordinary C style casts
do and that is where their virtue comes from. To restate it in other
terms, the C++ casts don't allow the range of casts that C casts do,
thus they help greatly in preventing accidental misuses of casts.


One of the other virtues to using them are their ugliness. Yes, I know
that most people (especially those with a C background) hate typing a
few extra keystrokes to achieve the same result, thus they stick to the
C style casts. But, in actuality, many casts used in C++ are not
needed, that is, they could be taked out with the implementation of
more thought-out code, thus using the C++ casts makes you think twice
about casting between different types and make you think if there is
some other way that you could achieve the same thing without using
them.
Also, in the same realm as the ugliness, they make it much easier to
perform a search of your code for any and all casts if at any time that
you need to, rather than being forced to search for type names or (ugh)
parenthesis.


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





PostPosted: Sat Mar 12, 2005 11:37 am    Post subject: Re: static_cast vs. C style cast Reply with quote



On 11 Mar 2005 21:08:18 -0500, "Vlad" <unknown (AT) host (DOT) com> wrote:

Quote:
Is there any real benefits of using static_cast instead of C-style cast
operation?

The static_cast and associated operators are easier to see exactly where a
change occurrs by enclosing an expression in parenthesis. While this isn't
better, it improves readability slightly - especially with inexperienced
programmers.

For example:
int i = (int)5.3 * 6;
int j = static_cast<int>(5.3) * 6;

Quote:
It seems to me that it doesn't do much except of introducing
restriction on casting const expressions into non-const. (well it also
doesn't allow to cast to the virtual base class...)

static_cast isn't meant to do that. You have to use one of dynamic_cast,
reinterpret_cast, or const_cast depending on the requirement. In some
cases, you need to define the casting operator so that the compiler will
know how to do it.

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

Back to top
Thomas Maeder
Guest





PostPosted: Sat Mar 12, 2005 11:39 am    Post subject: Re: static_cast vs. C style cast Reply with quote

"Vlad" <unknown (AT) host (DOT) com> writes:

Quote:
Is there any real benefits of using static_cast instead of C-style cast
operation? It seems to me that it doesn't do much except of introducing
restriction on casting const expressions into non-const. (well it also
doesn't allow to cast to the virtual base class...)

A C style cast can mean static_cast, const_cast, dynamic_cast,
reinterpret_cast, a combination thereof or something not expressable
by the C++ cast operators.

Using C++ cast operators therefore expresses the programmer's intent
better than writing a C style cast.


In my experience, casts often are where bugs are; making them stand
out by using C++ cast operators thus helps fixing errors.

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

Back to top
Ulrich Eckhardt
Guest





PostPosted: Sat Mar 12, 2005 11:43 am    Post subject: Re: static_cast vs. C style cast Reply with quote

Vlad wrote:
Quote:
Is there any real benefits of using static_cast instead of C-style cast
operation? It seems to me that it doesn't do much except of introducing
restriction on casting const expressions into non-const. (well it also
doesn't allow to cast to the virtual base class...)

I'd throw in that you also can't remove a volatile from an expression.
However, if you accept const correctness as useful, not being able to
accidentally cast it away with static_cast is a real benefit.

Uli

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !


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

Back to top
Bo Persson
Guest





PostPosted: Sat Mar 12, 2005 1:27 pm    Post subject: Re: static_cast vs. C style cast Reply with quote


"Vlad" <unknown (AT) host (DOT) com> skrev i meddelandet
news:1f4059b7274bac76408d699e7fe8914e (AT) localhost (DOT) talkaboutprogramming.com...
Quote:
Is there any real benefits of using static_cast instead of C-style
cast
operation? It seems to me that it doesn't do much except of
introducing
restriction on casting const expressions into non-const. (well it also
doesn't allow to cast to the virtual base class...)

One benefit is that it looks ugly, so you tend to avoid casting.

Another is that it is limited, so you can avoid unintentional casts,
like those offered by const_cast and reinterpret_cast.

This is all very much intentional!


The C++ way is to use converting constructors when you really want to
construct one type from another.


Bo Persson



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

Back to top
Michael Etscheid
Guest





PostPosted: Sat Mar 12, 2005 8:01 pm    Post subject: Re: static_cast vs. C style cast Reply with quote

Bo Persson wrote:
Quote:
This is all very much intentional!

What do you mean with this sentence?

Quote:
The C++ way is to use converting constructors when you really want to
construct one type from another.

Why should that be better? How do casts work in detail?

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


Back to top
Bo Persson
Guest





PostPosted: Sun Mar 13, 2005 12:36 pm    Post subject: Re: static_cast vs. C style cast Reply with quote


"Michael Etscheid" <the.michael.e (AT) gmail (DOT) com> skrev i meddelandet
news:d0uuup$2uc$04$1 (AT) news (DOT) t-online.com...
Quote:
Bo Persson wrote:
This is all very much intentional!

What do you mean with this sentence?

That the C++ committee had the intention of making casts look ugly and
be somewhat hard to use. The idea being that you should avoid using them
whenever possible.

Quote:

The C++ way is to use converting constructors when you really want to
construct one type from another.

Why should that be better? How do casts work in detail?

C style casts work by telling the compiler - do whatever necessary to
convert this value to that, possibly totally unrelated, other type. I
know what I'm doing, trust me! Honest, Gov.

Compiler says: Yeah, sure - it's your funeral!



Bo Persson



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

Back to top
Michael Etscheid
Guest





PostPosted: Mon Mar 14, 2005 12:02 am    Post subject: Re: static_cast vs. C style cast Reply with quote

Bo Persson schrieb:
Quote:
C style casts work by telling the compiler - do whatever necessary to
convert this value to that, possibly totally unrelated, other type. I
know what I'm doing, trust me! Honest, Gov.

Compiler says: Yeah, sure - it's your funeral!

But how do C++ casts work?

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


Back to top
Dave Harris
Guest





PostPosted: Mon Mar 14, 2005 12:08 am    Post subject: Re: static_cast vs. C style cast Reply with quote

[email]bop (AT) gmb (DOT) dk[/email] (Bo Persson) wrote (abridged):
Quote:
One benefit is that it looks ugly, so you tend to avoid casting.

The ugliness of new-style casts only discourages new-style casts. In my
experience, it doesn't discourage old-style casts. A lot of code uses
(int) purely because it is so much shorter than static_cast<int>().

-- Dave Harris, Nottingham, UK

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

Back to top
James Kanze
Guest





PostPosted: Mon Mar 14, 2005 1:00 am    Post subject: Re: static_cast vs. C style cast Reply with quote

Vlad wrote:

Quote:
Is there any real benefits of using static_cast instead of
C-style cast operation? It seems to me that it doesn't do much
except of introducing restriction on casting const expressions
into non-const. (well it also doesn't allow to cast to the
virtual base class...)

It prevents you from accidentally doing any number of things you
didn't want to do. I remember in one of my earliest C++
applications, there was a lot of casting up and down the
hierarchy. We rearranged the hierarchy, and some of those C
style casts changed from being static_cast (strictly up or down)
to being reinterpret_cast (sideways). We had a devil of a time
getting it all straightened out.

I still use the C style casts (or function style casts) when
what I want is a new object: A(1), for example, but also
double(i). Whenever there are alternative interpretations as to
what the conversion might mean (pointers or references),
however, I use only the new style casts.

--
James Kanze home: www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

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


Back to top
Seungbeom Kim
Guest





PostPosted: Mon Mar 14, 2005 2:12 pm    Post subject: Re: static_cast vs. C style cast Reply with quote

Thomas Maeder wrote:

Quote:
A C style cast can mean static_cast, const_cast, dynamic_cast,
reinterpret_cast, a combination thereof or something not expressable
by the C++ cast operators.

It can't be a dynamic_cast. 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.

--
Seungbeom Kim

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

Back to top
R Samuel Klatchko
Guest





PostPosted: Mon Mar 14, 2005 9:24 pm    Post subject: Re: static_cast vs. C style cast Reply with quote

Vlad wrote:
Quote:
Is there any real benefits of using static_cast instead of C-style cast
operation? It seems to me that it doesn't do much except of introducing
restriction on casting const expressions into non-const. (well it also
doesn't allow to cast to the virtual base class...)

Here are two benefits which I haven't seen anyone else mention.

1) Great for team coding standards. Since static_cast<> guarantees that
you are not not completely subverting the type system (since a C-style
cast could be the equivalent of a reinterpret_cast<>), coding guidelines
can have less strict rules for using static_cast<> then other casts.

2) Easy to find using basic text tools. If you've ever needed to find
all the casts in your code to review them (for example, when porting to
a 64 bit platform), you can easily locate the C++ casts with tools like
grep.

samuel

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


Back to top
Marco Manfredini
Guest





PostPosted: Mon Mar 14, 2005 9:30 pm    Post subject: Re: static_cast vs. C style cast Reply with quote

Vlad wrote:

Quote:
Is there any real benefits of using static_cast instead of C-style
cast operation? It seems to me that it doesn't do much except of
introducing restriction on casting const expressions into non-const.
(well it also doesn't allow to cast to the virtual base class...)

If you break static typing, you have to cast it and a static_cast<>
shows you where the weak limbs are and what exactly their condition is.

And second: Give a regular expression that matches a c-style cast. Give
another that matches static_cast. And now imagine, you have code review
tomorrow :-)

--
Marco


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

Back to top
Eric B
Guest





PostPosted: Tue Mar 15, 2005 8:28 pm    Post subject: Re: static_cast vs. C style cast Reply with quote

Vlad wrote:
Quote:
Is there any real benefits of using static_cast instead of C-style
cast
operation? It seems to me that it doesn't do much except of
introducing
restriction on casting const expressions into non-const. (well it
also
doesn't allow to cast to the virtual base class...)

In certain cases involving multiple inheritance, C-style casts can
produce incorrect results. e.g.:
class B1 {};
class B2 {};
class D : public B1, public B2 {};

void fn(D* pd)
{
B2* pb2 = (B2*) pd; // bad!
pb2 = static_cast<B2*> (pd); // works correctly
}

Eric B


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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.