casting question

  • Thread starter Thread starter dotnetchic
  • Start date Start date
D

dotnetchic

Can someone explain to me the difference between static_cast<T>(v) and
(what's the term for it?) old-style C cast?

UINT value1 = 0;

int value2 = (int)value1;
int value2 = static_cast<int>(value1);

TIA,
Sharon
 
dotnetchic said:
Can someone explain to me the difference between static_cast<T>(v) and
(what's the term for it?) old-style C cast?

UINT value1 = 0;

int value2 = (int)value1;
int value2 = static_cast<int>(value1);

static_cast is more explicit and more restrictive - you know on sight that a
static_cast does not:
- Cast away constness
- Cast between unrelated pointer types
- Reinterpret the bits of one type as another type

The C-style cast, however, is a chameleon - depending on the context, the
C-style (or function-style) cast is equivalent to either static_cast,
reinterpret_cast, or one of those two followed by const_cast.

So, in your example, there's no difference at all - the C-style cast in that
context is exactly the same as a static_cast. Use of the C++ casts is
prefereable because of the improved readability of the code, but there's no
functional (i.e. runtime or code generation) difference.

-cd
 
Thanks. Just what I was looking for.

I'm with you on the first part. The C++ cast is preferrable because
the function-style cast evaluates one of several other things. But
how, exactly, is
static_cast<int>(value)
more readable than
(int)(value)
?

I suppose I just have a bad taste for C++ with all its ugly angle
brackets and underscores :) Thanks for the info.

Sharon
 
dotnetchic said:
Thanks. Just what I was looking for.

I'm with you on the first part. The C++ cast is preferrable because
the function-style cast evaluates one of several other things. But
how, exactly, is
static_cast<int>(value)
more readable than
(int)(value)
?

Think about this - how would you search for c-style casts using your favorit
text editor? Of course, finding (int) is easy, but in general you can
declare a type in a cast expression, so what comes between the () can be
arbitrarily long and ugly. Finding static_cast< is always easy.
I suppose I just have a bad taste for C++ with all its ugly angle
brackets and underscores :) Thanks for the info.

You'lll grow to love them in time!

-cd
 
I'm with you on the first part. The C++ cast is preferrable because
the function-style cast evaluates one of several other things. But
how, exactly, is
static_cast<int>(value)
more readable than
(int)(value)
?

Clearly it's not ;)

But you can search for the new style casts easier!

Dave
 
Of course, finding (int) is easy, but in general you can
declare a type in a cast expression, so what comes between the () can be
arbitrarily long and ugly.

very true. I'm porting code right now that should go in the coding
horror hall of fame.
You'lll grow to love them in time!

I'll take your word on that.

Cheers!
Sharon
 
dotnetchic said:
Thanks. Just what I was looking for.

I'm with you on the first part. The C++ cast is preferrable because
the function-style cast evaluates one of several other things. But
how, exactly, is
static_cast<int>(value)
more readable than
(int)(value)
?

I suppose I just have a bad taste for C++ with all its ugly angle
brackets and underscores :) Thanks for the info.

They were made ugly and highly visible on purpose. You are supposed to
avoid them as much as possible!

When you have a code walkthru with your collegues, there is no chance
for them to miss asking you about this piece of code. "Why do you
think a cast is the best way to solve this problem?". Have the answer
ready!


Bo Persson
 
While I appreciate the advice, I'm referring to a large code base which
I am porting/maintaining; not that which I wrote. You are right,
casting should be avoided as much as possible, and I plan to do
something about that during the restructure of this project.

Regards,
Sharon
 
Back
Top