Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static_cast<int>(foo) vs. (int)foo

Tags:

c++

casting

Could somebody please elaborate on the differences?

like image 333
Evan Fosmark Avatar asked Jan 24 '09 10:01

Evan Fosmark


People also ask

What is difference between static_cast and Dynamic_cast?

static_cast − This is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coersion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc. dynamic_cast −This cast is used for handling polymorphism.

What does static_cast int do?

The static_cast operator converts variable j to type float . This allows the compiler to generate a division with an answer of type float . All static_cast operators resolve at compile time and do not remove any const or volatile modifiers.

Why is static_cast better than C style cast?

In short: static_cast<>() gives you a compile time checking ability, C-Style cast doesn't. static_cast<>() is more readable and can be spotted easily anywhere inside a C++ source code, C_Style cast is'nt. Intentions are conveyed much better using C++ casts.

What static_cast is actually doing?

In short, static_cast<> will try to convert, e.g., float-to-integer, while reinterpret_cast<> simply changes the compiler's mind to reconsider that object as another type.


3 Answers

The difference is that (int)foo can mean half a dozen different things. It might be a static_cast (convert between statically known types), it might be a const_cast (adding or removing const-ness), or it might be a reinterpret_cast (converting between pointer types)

The compiler tries each of them until it finds one that works. Which means that it may not always pick the one you expect, so it can become a subtle source of bugs.

Further, static_cast is a lot easier to search for or do search/replace on.

like image 83
jalf Avatar answered Oct 16 '22 21:10

jalf


Look at what Stroustrup has to say about that, including the following:

Because the C-style cast (T) can be used to express many logically different operations, the compiler has only the barest chance to catch misuses. [...]

The "new-style casts" were introduced to give programmers a chance to state their intentions more clearly and for the compiler to catch more errors. [...]

In particular, C++ makes the distinction between static_cast and reinterpret_cast:

The idea is that conversions allowed by static_cast are somewhat less likely to lead to errors than those that require reinterpret_cast. In principle, it is possible to use the result of a static_cast without casting it back to its original type, whereas you should always cast the result of a reinterpret_cast back to its original type before using it to ensure portability.

like image 45
Christoph Avatar answered Oct 16 '22 22:10

Christoph


(int) foo compares most to c++ reinterpret_cast<int>, i.e. no checks on the validity of the cast.

like image 1
Fredrik Jansson Avatar answered Oct 16 '22 21:10

Fredrik Jansson