static_cast better than C cast ?

  • Thread starter Vincent RICHOMME
  • Start date
V

Vincent RICHOMME

Is there any reason to use static_cast instead of old C syntax ?

Let's say I declare

GLfloat test = static_cast<GLfloat>(x);

or

GLfloat test = (GLfloat) x;


once compiled is the code different ?
 
V

Victor Bazarov

Vincent said:
Is there any reason to use static_cast instead of old C syntax ?

Let's say I declare

GLfloat test = static_cast<GLfloat>(x);

or

GLfloat test = (GLfloat) x;


once compiled is the code different ?

Depends on the relationship between GLfloat and the type of 'x'.

V
 
A

Alf P. Steinbach

* Vincent RICHOMME:
Is there any reason to use static_cast instead of old C syntax ?

Let's say I declare

GLfloat test = static_cast<GLfloat>(x);

or

GLfloat test = (GLfloat) x;

once compiled is the code different ?

Wrong question. :)

However, Victor already answered that.

Right question 1: what kinds of errors do static_cast help identify
(i.e. what code will not compile)?

Answer: all kinds of casts that are not static casts, namely reinterpret
casts, const casts and casting to inaccessible base.

Right question 2: what does static_cast communicate to the reader of the
code?

Answer: that you're intending to cast between related types, namely that
the types are assumed to be related in one of the ways supported by
static_cast, and also that you're competent enough to know the
difference (which is very important when maintaining code and making
decisions about what to investigate, i.e is that dubious construct most
probably intentional, a bug, or just arbitrary meaningless stuff?).

Right question 3: what other advantages are there?

Answer: for example, it's much easier to search for static_cast than to
search for a C style cast or a C++ "pseudo constructor call" cast.
 
R

red floyd

Victor said:
Depends on the relationship between GLfloat and the type of 'x'.

I couldn't find a FAQ on C++ style casts vs. C style casts.

In general, you should *prefer* static_cast instead of C casts.
They're easier to find, they're safer, etc...
 
R

red floyd

Vincent said:
Is there any reason to use static_cast instead of old C syntax ?

Let's say I declare

GLfloat test = static_cast<GLfloat>(x);

or

GLfloat test = (GLfloat) x;

I couldn't find a FAQ on C++ style casts vs. C style casts (but it
should be one).

In general, you should *prefer* static_cast instead of C casts.
They're easier to find, they're safer, etc...
 
F

faceman28208

Is there any reason to use static_cast instead of old C syntax ?

Let me give a contrarian view. When the C++ standard came out, I
became a loyal user of the new style casts.

However, over the years I have reverted to the olde style (with the
exception of dynamic_cast). The reason is because most of the time I
am using casts is for calls to libraries. I find that

(void*) &x

is clearer than the multiple casts required with the new format:

reinterprest_cast<void*>(const_cast><int*>(&x)) ;

I find the that the latter is less clear than the former. For
consistency, I just use the former all the time. If I did a lot of
searching for casts, I might consider the the new style to be and
advantage. Since I don't, I have come to regard the new style casts as
being primarily an academic distinction.
once compiled is the code different ?

No way to tell but probably not.
 
V

Victor Bazarov

Let me give a contrarian view. When the C++ standard came out, I
became a loyal user of the new style casts.

However, over the years I have reverted to the olde style (with the
exception of dynamic_cast). The reason is because most of the time I
am using casts is for calls to libraries. I find that

(void*) &x

is clearer than the multiple casts required with the new format:

reinterprest_cast<void*>(const_cast><int*>(&x)) ;

Replace 'void' with 'char' and I'll believe it.
I find the that the latter is less clear than the former. [..]

V
 
M

mlimber

Let me give a contrarian view. When the C++ standard came out, I
became a loyal user of the new style casts.

However, over the years I have reverted to the olde style (with the
exception of dynamic_cast). The reason is because most of the time I
am using casts is for calls to libraries. I find that

(void*) &x

is clearer than the multiple casts required with the new format:

reinterprest_cast<void*>(const_cast><int*>(&x)) ;

You don't need two casts here. Since any non-const pointer can be
implicitly converted to a void*, the const_cast will suffice. The
advantage of using const_cast over a C-style cast is that it makes
clear precisely why you are casting away (although it is a tad less
helpful when casting away volatile -- cv_cast, anyone?).
I find the that the latter is less clear than the former. For
consistency, I just use the former all the time. If I did a lot of
searching for casts, I might consider the the new style to be and
advantage. Since I don't, I have come to regard the new style casts as
being primarily an academic distinction.

I don't think it's academic. The new style casts are safer than the
other at no run-time cost to you (except with dynamic_cast, of
course). Casts should generally be avoided, but when they're needed
(as in a call to an olde-style library), they make the reason for the
cast clearer. See Alf's post above, and see the Creator's FAQ:

http://www.research.att.com/~bs/bs_faq2.html#static-cast

Cheers! --M
 
J

James Kanze

I couldn't find a FAQ on C++ style casts vs. C style casts.
In general, you should *prefer* static_cast instead of C casts.
They're easier to find, they're safer, etc...

For pointers or references, I agree; I wouldn't let code through
code review that used a C style cast for a pointer or a
reference. For other things, it's less obvious. A lot of
people use function style casts for class types, e.g.:
"MyClass( x )"---unless the type conversion has exactly one
argument, its the only style of cast which is legal. And I (and
others I've worked with) have nothing against the C style casts
for arithmetic types, particularly for widening casts (e.g.
(double)someInt or (unsigned long)someShort).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top