Drew Noakes
Drew Noakes

Reputation: 310897

Difference between static casting styles in C++ for primitive values

Given:

int i = some_int_value();

double d1 = i;
double d2 = (double)i;    
double d3 = double(i);
double d4 = static_cast<double>(i);

Is there any difference between these casts in reality?

What if the cast was narrowing (double to int) instead of widening?

I've read that C++ should avoid use of C-style casts, though I'm not sure this applies to primitive values.

Upvotes: 0

Views: 305

Answers (6)

Howard Rubin
Howard Rubin

Reputation: 360

double d1 = i;

This will warn you if there could be a data loss.

double d2 = (double)i;    

This is a C style cast. @faranwath is correct that this is like reinterpret_cast, but also does a const_cast if needed.

double d3 = double(i);

This calls the double constructor with int argument, also will warn if data could be lost.

double d4 = static_cast<double>(i);

This suppresses the compiler warning and is easy to search for in your code. For my taste, this most clearly expresses your intent. In other cases than int->double, such as if const_cast were neeeded, the compiler would alert you to the problem with an error. That's a good thing.

Upvotes: 1

user2033018
user2033018

Reputation:

The main issue is that a C-style cast will do anything to get its job done, even a conversion between unrelated types. static_cast has limited power, so if you try to do anything it can't do, you get an error.

You could say a C-style cast is like reinterpret_cast, except that you cannot cast away qualifiers like const.

Upvotes: 2

Jeremy Villegas
Jeremy Villegas

Reputation: 193

As faranwath said, C-style casts will do anything to get the job done. The major difference is that static_cast is safe and tells the compiler exactly what you want to do and no more.

Upvotes: 0

Zac Howland
Zac Howland

Reputation: 15872

double d1 = i;

This will use implicit casting.

double d2 = (double)i;    
double d3 = double(i);

Those are identical C-style explicit casts.

double d4 = static_cast<double>(i);

This is the C++-style explicit cast.

From a practical standpoint, all of the explicit casts will do the same thing. The static_cast operator has some checks to ensure the cast is actually valid (it will not compile if the cast is invalid) and makes it easier to find casts in your code. If you wanted to force a cast (even if it is unsafe), you can use reinterpret_cast, which will trust you to know what you are doing (no error at compile time for invalid casts - and potentially undefined behavior if you do something really off the wall).

Upvotes: 1

Brian Bi
Brian Bi

Reputation: 119144

In this case, these are all equivalent. They all perform a standard conversion, in particular a floating-integral conversion. They would also be equivalent if converting from double to int.

Upvotes: 2

Christian Hackl
Christian Hackl

Reputation: 27528

In this case, they are equivalent. You should prefer the C++-style casts (static_cast, reinterpret_cast, dynamic_cast and const_cast) for all types, no matter if primitive or not, because you want to state your exact intention. The C-style cast is mightier than the others (taken individually) and should be avoided exactly for that reason.

If you apply a static_cast where it is not allowed, the compiler will complain. If you apply a C-style cast in the same situation, you may, for example, silently get a reinterpret_cast with undefined run-time behaviour instead of a compiler error.

Upvotes: 3

Related Questions