473,836 Members | 1,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is static_cast really as fast as C/C++ style casts?

Hi all. In short, is there any performance difference between:

float f = 10.0f;
int i = static_cast<int >(f);

and

float f = 10.0f;
int i = int(f);

I've been meaning to ask this for a while but just how fast is
static_cast? I had always assumed (without proof) that static_cast is
implemented as a template which just wraps a C/C++ style cast.But then
it the Josuttis book it says:

"The conversion is allowed only if a type conversion is defined"

Presumably then this test is done at compile time?

Also, if static_cast is just a wrapper around C/C++ casts then how does
it differ from reinterpret_cas t? Maybe these things are compiler
dependant but any info is useful.

Thanks,

David

Sep 24 '06 #1
11 18284
Hi all. In short, is there any performance difference between:

float f = 10.0f;
int i = static_cast<int >(f);

and

float f = 10.0f;
int i = int(f);

No, because a "cast" is a compile-time concept -- not a runtime concept.
Both should produce identical machine code.

I've been meaning to ask this for a while but just how fast is
static_cast?

It doesn't have a speed.

I had always assumed (without proof) that static_cast is implemented as
a template which just wraps a C/C++ style cast.But then it the Josuttis
book it says:

"The conversion is allowed only if a type conversion is defined"

Presumably then this test is done at compile time?

Yes it is. Test it with a conforming compiler:

char *p = 0;

long i = static_cast<lon g>(p);

You should get a compiler error.
Also, if static_cast is just a wrapper around C/C++ casts then how does
it differ from reinterpret_cas t? Maybe these things are compiler
dependant but any info is useful.

In the way I showed you above. There's a list of things which static_cast
WILL let you do, and others which it WON'T let you do. For the things it
won't let you do, you must use reinterpret_cas t.

--

Frederick Gotham
Sep 24 '06 #2
es*****@googlem ail.com wrote :
Presumably then this test is done at compile time?
Yes.
>
Sep 24 '06 #3
Ok, thanks for the input. I'm keen to write correct code but don't want
to sacrifice speed if possible. Seems like I'm ok here :-D

es*****@googlem ail.com wrote:
Hi all. In short, is there any performance difference between:

float f = 10.0f;
int i = static_cast<int >(f);

and

float f = 10.0f;
int i = int(f);

I've been meaning to ask this for a while but just how fast is
static_cast? I had always assumed (without proof) that static_cast is
implemented as a template which just wraps a C/C++ style cast.But then
it the Josuttis book it says:

"The conversion is allowed only if a type conversion is defined"

Presumably then this test is done at compile time?

Also, if static_cast is just a wrapper around C/C++ casts then how does
it differ from reinterpret_cas t? Maybe these things are compiler
dependant but any info is useful.

Thanks,

David
Sep 24 '06 #4
es*****@googlem ail.com schrieb:
Ok, thanks for the input. I'm keen to write correct code but don't want
to sacrifice speed if possible. Seems like I'm ok here :-D
Read this interesting article:
http://www.flounder.com/optimization.htm

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 24 '06 #5
Frederick Gotham wrote:
No, because a "cast" is a compile-time concept -- not a runtime concept.
Not always, think about dynamic_cast. Someone can debate if is the same
concept of casting that the others cast, but his name in C++ is clear.

--
Salu2
Sep 24 '06 #6
Julián Albo wrote:
Frederick Gotham wrote:
>No, because a "cast" is a compile-time concept -- not a runtime concept.

Not always, think about dynamic_cast. Someone can debate if is the same
concept of casting that the others cast, but his name in C++ is clear.
A cast is something that exists in source code to tell the compiler to
do a conversion. Some conversions can be done implicitly, and others
will only be done if you tell the compiler with a cast. Some conversions
require runtime code, but that's independent of whether the conversion
requires a cast. In some circumstances, dynamic_cast does not require
modifying the pointer value. In some circumstances, static_cast does. In
some circumstances, initializing a pointer to base from a pointer to
derived (without a cast) modifies the pointer value; in some it does not.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 24 '06 #7
es*****@googlem ail.com wrote:
>
I've been meaning to ask this for a while but just how fast is
static_cast? I had always assumed (without proof) that static_cast is
implemented as a template which just wraps a C/C++ style cast.
static_cast is not a template. It's a keyword, and the compiler
generates whatever code is appropriate. The difference between
static_cast and a C-style cast is that there are some conversions that
you can do with a C-style cast that you can't do with a static_cast.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 24 '06 #8
Pete Becker wrote:
>>No, because a "cast" is a compile-time concept -- not a runtime concept.
Not always, think about dynamic_cast. Someone can debate if is the same
concept of casting that the others cast, but his name in C++ is clear.

A cast is something that exists in source code to tell the compiler to
do a conversion. Some conversions can be done implicitly, and others
will only be done if you tell the compiler with a cast. Some conversions
require runtime code, but that's independent of whether the conversion
requires a cast. In some circumstances, dynamic_cast does not require
modifying the pointer value. In some circumstances, static_cast does. In
But the runtime nature of dynamic_cast is more relevant. With other casts
you essentially say "I know the conversion is valid". With dynamic_cast you
say "check at runtime if the conversion is possible".

--
Salu2
Sep 24 '06 #9
Julián Albo wrote:
Pete Becker wrote:
>>>No, because a "cast" is a compile-time concept -- not a runtime concept.
Not always, think about dynamic_cast. Someone can debate if is the same
concept of casting that the others cast, but his name in C++ is clear.
A cast is something that exists in source code to tell the compiler to
do a conversion. Some conversions can be done implicitly, and others
will only be done if you tell the compiler with a cast. Some conversions
require runtime code, but that's independent of whether the conversion
requires a cast. In some circumstances, dynamic_cast does not require
modifying the pointer value. In some circumstances, static_cast does. In

But the runtime nature of dynamic_cast is more relevant. With other casts
you essentially say "I know the conversion is valid". With dynamic_cast you
say "check at runtime if the conversion is possible".
I just re-read what I wrote, and, unfortunately, left out something
important: sometimes a dynamic_cast does not require any runtime check.
Converting a pointer to T into a pointer to T, for a silly example. But
you can also use a dynamic_cast to convert a pointer to derived into a
pointer to base, and that needs only a compile-time check.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 24 '06 #10

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

Similar topics

7
7385
by: buds | last post by:
Hi all, Following is the assigment operator of a derived class Derived& Derived::operator=(const Derived& inDerived) { //to assign to the base class object the following statement static_cast<Base&>(*this)=inDerived;//works fine
2
2137
by: Alexander Stippler | last post by:
In some situations I can apply a C-style cast, but no static_cast. I'm not quite sure about the differences in such places. A little example: template <typename T> class Dummy { public: Dummy(int i) {} };
11
5164
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 one type to another. However, I'm certain that there is something else that is happening. Can someone explain the difference or recommend an online site that can explain it to me?
9
4175
by: news.ir.com.au | last post by:
Hi, In the following code I get the compiler error: error C2243: 'static_cast' : conversion from 'class B *' to 'class A *' exists, but is inaccessible I understand why I get this error and can usually get around the situation by inserting a "using A::..." statement inside class B, however, due to this being a static cast, what is the syntax?
26
2565
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 favor using a static_cast. Is there any technical reason to favor the C-style over a static_cast? http://doc.coin3d.org/Coin/index.html void foo(SoNode * node) { if (node->getTypeId() == SoFile::getClassTypeId()) {
24
4775
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);
9
3183
by: Vincent RICHOMME | last post by:
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;
4
2473
by: subramanian100in | last post by:
when are the use of static_cast and const_cast essential ? Stroustrup, in his book TC++PL(3rd edition) in page number 139 (in Section 6.5 Advice - ), has advised "Avoid explicit type conversion(casts)" Given this, why do we have static_cast and const_cast provided by the language ? Kindly explain
5
3919
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 static_cast<>? In other words, is there any real difference between statements like (with non-pointer types): double a = 3.4; int b = (int)a; // <--- this
0
10835
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10541
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10585
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10249
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9367
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7785
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5645
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4447
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3108
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.