Mega Search
23.2 Million


Sign Up

Make a donation  
Re: static_cast vs. C style cast  
News Group: comp.lang.c++.moderated

Vlad wrote:
> 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! ]


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 14-Mar-2005, at 4:24 PM EST
From: R Samuel Klatchko
 
Re: static_cast vs. C style cast  
News Group: comp.lang.c++.moderated
Vlad wrote:

> 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! ]

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 14-Mar-2005, at 4:30 PM EST
From: Marco Manfredini
 
Re: static_cast vs. C style cast  
News Group: comp.lang.c++.moderated
Vlad wrote:
> 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 (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! ]

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 15-Mar-2005, at 3:28 PM EST
From: Eric B
 
Re: static_cast vs. C style cast  
News Group: comp.lang.c++.moderated
Hi,

> 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 (pd); // works correctly
> }

Sorry, but I don't understand this example. What you do need the cast for here?

B2 *pb2 = pd;

should do that right away.

So long,
 	Thomas


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

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 16-Mar-2005, at 9:46 PM EST
From: Thomas Richter
 
Re: static_cast vs. C style cast  
News Group: comp.lang.c++.moderated
Eric B wrote:

> > 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 (pd); // works correctly
> }

Since when?  This is required to work by the standard, and has
always worked in every compiler I've used (at least since
multiple inheritance was introduced).  What doesn't work here is
to convert a B1* inton a B2*.  It's not a legal static_cast,
but the C style cast doesn't care -- if a static_cast can't get
the job done, a reinterpret_cast will.

--
James Kanze                                           GABI Software
Conseils en informatique orientée objet/
                    Beratung in objektorientierter Datenverarbeitung
9 place 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! ]

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 17-Mar-2005, at 3:15 AM EST
From: r
 
Re: static_cast vs. C style cast  
News Group: comp.lang.c++.moderated
Thomas Richter wrote:

>> 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 (pd); // works correctly
>> }

> Sorry, but I don't understand this example. What you do need
> the cast for here?

> B2 *pb2 = pd;

> should do that right away.

And:

     B2* pb2 = pd ;
     B2* pb2 = (B2*)pd ;
     B2* pb2 = static_cast< B2* >( pd ) ;

all have exactly the same effect.

I think the case he was worried about was actually:

     B1* pb1 = pd ;
     B2* pb2 = (B2*)pb1 ;

In this case, the C style cast does NOT result in anything
usable -- it's a reinterpret_cast.  And this is really THE
telling argument in favor of new style casts -- if you use
static_cast here, the code doesn't compile.  The compiler tells
you've made a mistake (e.g in using pb1 when you wanted pd);
with a C style cast, you're in for some rather long debugging
sessions.

--
James Kanze                                           GABI Software
Conseils en informatique orientée objet/
                    Beratung in objektorientierter Datenverarbeitung
9 place 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! ]

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 17-Mar-2005, at 8:11 AM EST
From: r