473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

static_cast better than C cast ?

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 ?
May 2 '07 #1
9 3147
Vincent RICHOMME wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 2 '07 #2
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 2 '07 #3
Victor Bazarov wrote:
Vincent RICHOMME wrote:
>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'.
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...
May 2 '07 #4
Vincent RICHOMME wrote:
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...
May 2 '07 #5
On May 2, 2:13 pm, Vincent RICHOMME <richo...@free.frwrote:
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.

May 2 '07 #6
fa**********@yahoo.com wrote:
On May 2, 2:13 pm, Vincent RICHOMME <richo...@free.frwrote:
>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)) ;
Replace 'void' with 'char' and I'll believe it.
I find the that the latter is less clear than the former. [..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 2 '07 #7
On May 2, 2:49 pm, faceman28...@yahoo.com wrote:
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_f...ml#static-cast

Cheers! --M

May 2 '07 #8
Victor Bazarov a écrit :
Vincent RICHOMME wrote:
>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
x is a int and GLFloat is a float
May 2 '07 #9
On May 2, 8:33 pm, red floyd <no.s...@here.dudewrote:
Victor Bazarov wrote:
Vincent RICHOMME wrote:
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'.
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).

--
James Kanze (GABI Software) email:ja*********@gmail.com
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
May 3 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Wenjie | last post by:
Hello, I have a class B to mediate/isolate between implementation and their users (like used in an abstract factory class pattern): // B.h class B { static B* getImpl(); static void...
11
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
26
by: Steven T. Hatton | last post by:
The code shown below is an example from the Coin3D documentation. I believe the use of the C-style cast is safe under the circumstances, but from what I've been exposed to (TC++PL(SE)), I would...
3
by: shrishjain | last post by:
Hi All, Do people frequently use static_cast, const_cast etc in industry?.. I only saw them in books, and never in real code.. Shrish
6
by: bonham | last post by:
i m a beginner in C++. I have some questions: what is static_cast<unsigned>? i saw someone use static_cast<unsigned> and static_cast<unsigned char> together. why? e.g....
19
by: PengYu.UT | last post by:
I see some code use static_cast<some_pointer_type>(0) instead of NULL to describe null pointer. I'm wondering what is the pros and cons of each way. Is there any reason why we should one verses the...
24
by: Rahul | last post by:
Hi, I have a class A : public B {...member functions......data members}; and am doing the following A *p=new A(); void *p=static_cast<void *>(p); factory_instance->process(p);
2
by: Lucy Ludmiller | last post by:
I have two classes: class base { ... }; and class derived : public base { ... }; I have a function :
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.