Type Casting

Started by
9 comments, last by bytecoder 19 years, 8 months ago
I have a quick question about type casting... I've been used to type casting like this:

float fFloat;
int iInt = 10;
fFloat = float(iInt);
And I have a feeling that doing this is a bad idea, yes or no? I think the correct way is:

float fFloat;
int iInt = 10;
fFloat = (float)iInt;
Is there a difference? I have a feeling that the first example will create a new temporary variable in memory then delete it, while the second example will not (thus improving performance). If that is the case, is the compiler smart enough to optimise this and not create a temporary variable?
Advertisement
In C, the correct way is (float)x;.

In C++, the correct way is static_cast<float>(x); - though the (float)x (C-style cast) and float(x) (conversion constructor) syntaxes are also possible.

Quote:I have a feeling that the first example will create a new temporary variable in memory then delete it, while the second example will not (thus improving performance).


Either way, there shouldn't be any difference - a temporary variable will be required in both cases, though compilers may optimize it. Whether they do it or not is dependent on a wide variety of factors, including in particular the actual types you are 'casting'.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
In C++, the correct way is static_cast<float>(x); - though the (float)x (C-style cast) and float(x) (conversion constructor) syntaxes are also possible.

Either way, there shouldn't be any difference - a temporary variable will be required in both cases, though compilers may optimize it.


Do people really use static_cast<float>(x)... seems like a lot more typing than just (float)x or float(x)?

So anyway, can anybody confirm this? (float)x and float(x) is the same performance-wise? It will save me a lot of time going through my programs fixing it up :) Thanks.
Quote:Original post by SpaceDude
So anyway, can anybody confirm this? (float)x and float(x) is the same performance-wise? It will save me a lot of time going through my programs fixing it up :) Thanks.


(float)x is a cast, float(x) is constructing a float with the value x. Ultimately they do the same thing, and generate the same code. I would use (float)x since it would also work with C, but it is a matter of taste.

The static_cast also does the same thing. Some may say it is more 'correct', but frankly it doesn't matter here.
Quote:Original post by SpaceDude
Do people really use static_cast<float>(x)... seems like a lot more typing than just (float)x or float(x)?


Yes, and it is intentional. You should think twice (three times, really) before performing a cast - any cast. It is also easier to notice in a chunk of code. Not to mention that it is not strictly equivalent to a C cast, offering you a finer granularity in the correctness-checking of your program (static_cast can only cast between related types, cannot remove const, etc.) and better express the exact intent of the programmer.

Quote:So anyway, can anybody confirm this? (float)x and float(x) is the same performance-wise?


int-float conversions are standard conversions. Your code snippets don't even need the cast. If used within an expression, both forms will merely create an unnamed temporary float (i.e. load the value in a FP register).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
As far as im aware, an IntFloat conversion will require a temp variable anyway since floating point numbers are stored in a different format. I would guess that SignedUnsigned and shortlong typecasts won't though.
Performance wise, there shouldn't be much, in it, depending on compiler. However, since Float - Int conversions are costly, it would be better to try to avoid the type cast if at all possible in performance critical code.
Quote:Original post by Willm
(float)x is a cast, float(x) is constructing a float with the value x.


No, C++ defines both to be exactly the same thing: a cast. See section 5.2.3 and section 5.4 in the C++ standard.
Quote:Original post by Fruny
Your code snippets don't even need the cast.


Yeh but then i get all those ugly warnings!

Thanks SiCrane, had a quick search for "the C++ standard" but couldn't find it. I'll take your word for it though.
Quote:Original post by SpaceDude
Yeh but then i get all those ugly warnings!


Warnings or not, the compiler will still do a cast.

Quote:Thanks SiCrane, had a quick search for "the C++ standard" but couldn't find it. I'll take your word for it though.


Here or there.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement