Could somebody please elaborate on the differences?
-
3exact duplicate of http://stackoverflow.com/questions/103512/in-c-why-use-staticcastintx-instead-of-intx – jalf Jan 24 '09 at 10:11
-
Does this answer your question? [Why use static\_cast
(x) instead of (int)x?](https://stackoverflow.com/questions/103512/why-use-static-castintx-instead-of-intx) – Mariia Illarionova Dec 18 '19 at 14:56
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.

- 243,077
- 51
- 345
- 550
-
C-style casts can't do "safe" downcasting like dynamic_cast, only "unsafe" downcasting like static_cast. – bk1e Jan 24 '09 at 17:12
-
1Also, I put "safe" and "unsafe" in quotes because nothing in C++ is truly safe. – bk1e Jan 24 '09 at 17:15
-
" (int)foo ... might be a dynamic_cast (downcasting to derived class)" Not true. (int)foo will never turn into a dynamic cast. – Suma Jan 24 '09 at 17:50
-
-
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.
(int) foo compares most to c++ reinterpret_cast<int>
, i.e. no checks on the validity of the cast.

- 3,764
- 3
- 30
- 33
-
http://www.velocityreviews.com/forums/t280611-reinterpretcastltgt-v-staticcastltgt.html expains the difference between static_cast and reinterpret_cast – Fredrik Jansson Jan 24 '09 at 10:14
-
No, it compares to a combination of all the C++ casts. It'll perform the first type of cast that's valid. – jalf Jan 24 '09 at 10:14
-
-
Are you trying to say that the problem with (int)foo is that it probably falls through to reinterpret_cast
most times? So you don't actually get the benefit of static type checking. – Brian Matthews Jan 24 '09 at 10:23 -
This answer is wrong. reinterpret_cast can, as other c++ casts, do only very little compared to a c-style cast. – Johannes Schaub - litb Jan 24 '09 at 14:47