https://en.cppreference.com/w/cpp/language/explicit_cast Basically, it is equivalent to a C-style cast (int) d, which does consider static_cast, but could also do a reinterpret_cast if valid. Answer from marko312 on reddit.com
🌐
Reddit
reddit.com › r/cpp_questions › is `static_cast(y)` the same as `x(y)` for simple data types like `int` and `double`?
r/cpp_questions on Reddit: Is `static_cast<X>(y)` the same as `X(y)` for simple data types like `int` and `double`?
August 11, 2020 -

Say I had the lines:

double d = 1.1;
auto a = int(d);
auto b = static_cast<int>(d);

I understand that I'm doing a cast of a double to an in for the creation of variable b, but is that also happening as well for the creation of a? If I can use is as a shorthand for doing static casts, I'd love to. But if that constructor style variable creation isn't actually doing a static cast, I'd like to know too.

EDIT:

I would say this is my favourite answer of the bunch: https://www.reddit.com/r/cpp_questions/comments/i76d6e/is_static_castxy_the_same_as_xy_for_simple_data/g119fnu/

Discussions

int vs. typecast - C++ Forum
Code/IDE output below. The tutorial was extolling the virtues of using static_cast over integer assignment in converting integer to ASCII. I have both methods listed below. I want to know why the second is "better" then the first. More on cplusplus.com
🌐 cplusplus.com
static_cast vs (int)....
If you're using c++ I'd use static_cast or one of the other C++ casting type. static_cast is a little smarter then C style casts. It will do some simple type checking. Any help I can get the compiler to do for me is good in my book. C style casts are completely ignorant. More on gamedev.net
🌐 gamedev.net
6
March 1, 2025
static_cast vs (int)....
using static_cast reinterpret_cast const_cast and dynamic_cast instead of c-style casts is, I believe, primarily just because they are more specific and catch dangerous behavior (warnings/errors) better than just a c-style cast does. More on gamedev.net
🌐 gamedev.net
6
November 23, 2021
Why use static_cast over C style cast for primitive data types?
Static cast is "safer" as it has more compile-time checks. i.e. You might be accidentally casting away constness or mis-align a pointer. A c-style cast is really a combo of reinterpret_cast and static_cast, and most people won't know which one the compiler will choose (including the person who wrote that code), so why not write in the code which one we want in the first place and save all of the headache? edit: Also there's a million SO questions about this :) https://stackoverflow.com/questions/103512/why-use-static-castintx-instead-of-intx https://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used More on reddit.com
🌐 r/cpp_questions
45
21
February 11, 2020
Top answer
1 of 9
765

The main reason is that classic C casts make no distinction between what we call static_cast<>(), reinterpret_cast<>(), const_cast<>(), and dynamic_cast<>(). These four things are completely different.

A static_cast<>() is usually safe. There is a valid conversion in the language, or an appropriate constructor that makes it possible. The only time it's a bit risky is when you cast down to an inherited class; you must make sure that the object is actually the descendant that you claim it is, by means external to the language (like a flag in the object). A dynamic_cast<>() is safe as long as the result is checked (pointer) or a possible exception is taken into account (reference).

A reinterpret_cast<>() (or a const_cast<>()) on the other hand is always dangerous. You tell the compiler: "trust me: I know this doesn't look like a foo (this looks as if it isn't mutable), but it is".

The first problem is that it's almost impossible to tell which one will occur in a C-style cast without looking at large and disperse pieces of code and knowing all the rules.

Let's assume these:

class CDerivedClass : public CMyBase {...};
class CMyOtherStuff {...} ;

CMyBase  *pSomething; // filled somewhere

Now, these two are compiled the same way:

CDerivedClass *pMyObject;
pMyObject = static_cast<CDerivedClass*>(pSomething); // Safe; as long as we checked

pMyObject = (CDerivedClass*)(pSomething); // Same as static_cast<>
                                     // Safe; as long as we checked
                                     // but harder to read

However, let's see this almost identical code:

CMyOtherStuff *pOther;
pOther = static_cast<CMyOtherStuff*>(pSomething); // Compiler error: Can't convert

pOther = (CMyOtherStuff*)(pSomething);            // No compiler error.
                                                  // Same as reinterpret_cast<>
                                                  // and it's wrong!!!

As you can see, there is no easy way to distinguish between the two situations without knowing a lot about all the classes involved.

The second problem is that the C-style casts are too hard to locate. In complex expressions it can be very hard to see C-style casts. It is virtually impossible to write an automated tool that needs to locate C-style casts (for example a search tool) without a full blown C++ compiler front-end. On the other hand, it's easy to search for "static_cast<" or "reinterpret_cast<".

pOther = reinterpret_cast<CMyOtherStuff*>(pSomething);
      // No compiler error.
      // but the presence of a reinterpret_cast<> is 
      // like a Siren with Red Flashing Lights in your code.
      // The mere typing of it should cause you to feel VERY uncomfortable.

That means that, not only are C-style casts more dangerous, but it's a lot harder to find them all to make sure that they are correct.

2 of 9
132

One pragmatic tip: you can search easily for the static_cast keyword in your source code if you plan to tidy up the project.

🌐
TutorialsPoint
tutorialspoint.com › article › why-use-static-cast-int-x-instead-of-int-x-in-cplusplus
Why use static_cast<int>(x) instead of (int)x in C++?
February 11, 2020 - The (int)x is C-style typecasting, where static_cast<int>(x) is used in C++. This static_cast<>() gives a compile-time checking facility, but the C-style casting does not support that. This static_cast<>() can be spotted anywhere inside a C++ code.
🌐
Cplusplus
cplusplus.com › forum › general › 171720
int vs. typecast - C++ Forum
> I want to know why the second is "better" then the first That static_cast<int>(c) is 'better' than int(c) or +c is a matter of opinion. For the record, I do not agree with it.
🌐
Learn C++
learncpp.com › cpp-tutorial › introduction-to-type-conversion-and-static_cast
4.12 — Introduction to type conversion and static_cast – Learn C++
October 21, 2021 - It’s worth noting that the argument to static_cast evaluates as an expression. When we pass in a variable, that variable is evaluated to produce its value, and that value is then converted to the new type. The variable itself is not affected by casting its value to a new type. In the above case, variable ch is still a char, and still holds the same value even after we’ve cast its value to an int.
🌐
TutorialsPoint
tutorialspoint.com › article › what-is-the-difference-between-static-cast-and-c-style-casting
What is the difference between static_cast<> and C style casting?
August 3, 2021 - Here we will see what are the differences between static_cast and normal C style cast. The normal cast like (int)x is C style typecasting where static_cast (x) is used in C++.
Find elsewhere
🌐
INFN
fe.infn.it › u › paterno › Geant4_tutorial › C++ resources › static_cast.pdf pdf
Why use static_cast<int>(x) instead of (int)x?
March 4, 2025 - Why use static_cast<int>(x) instead of (int)x? The main reason is that classic C casts make no distinction between what we call static_cast<>(), reinterpret_cast<>(), const_cast<>(), and dynamic_cast<>(). These four things are completely · different. A static_cast<>() is usually safe.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › static_cast-in-cpp
static_cast in C++ - GeeksforGeeks
July 11, 2025 - This is the simplest type of cast that can be used. It is a compile-time cast. It does things like implicit conversions between types (such as int to float, or pointer to void*), and it can also call explicit conversion functions. ... The return value of static_cast will be of dest_type.
🌐
GameDev.net
gamedev.net › forums › topic › 356627-static_cast-vs-int › 3339599
static_cast vs (int).... - General and Gameplay Programming - GameDev.net
March 1, 2025 - A c-style cast attempts to change one object type into another [through use of inbuilt conversion routines, or the conversion routines that you build into your objects through things like 'operator int()'] The static casts can also do this, but don't like casting pointers in general, unless there is an inbuilt conversion or one object is derived from the other.
🌐
GeeksforGeeks
geeksforgeeks.org › static_cast-in-c-type-casting-operators
static_cast in C++ | Type Casting operators - GeeksforGeeks
February 6, 2023 - This means that even if you think you can some how typecast a particular object int another but its illegal, static_cast will not allow you to do this.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › cpp › static-cast-operator
static_cast Operator | Microsoft Learn
October 21, 2021 - In general you use static_cast when you want to convert numeric data types such as enums to ints or ints to floats, and you are certain of the data types involved in the conversion. static_cast conversions are not as safe as dynamic_cast ...
🌐
Cppreference
en.cppreference.com › w › cpp › language › static_cast.html
static_cast conversion - cppreference.com
August 18, 2024 - one is a standard-layout class object and the other is the first non-static data member of that object or any base class subobject of that object, or · there exists an object c such that a and c are pointer-interconvertible, and c and b are pointer-interconvertible.
🌐
GameDev.net
gamedev.net › forums › topic › 356627-static_cast-vs-int › 3339597
static_cast vs (int).... - General and Gameplay Programming - GameDev.net
November 23, 2021 - A c-style cast attempts to change one object type into another [through use of inbuilt conversion routines, or the conversion routines that you build into your objects through things like 'operator int()'] The static casts can also do this, but don't like casting pointers in general, unless there is an inbuilt conversion or one object is derived from the other.
Top answer
1 of 9
65

First, understand that those lines are not equivalent.

int* anInt = (int*)aFloat; // is equivalent to

int* anInt = reinterpret_cast<int*>(aFloat); 

What is happening here is that the programmer is asking the compiler to do whatever it can to make the cast.

The difference is important because using static_cast will only ask for a base type that is "safe" to convert to, where reinterpret_cast will convert to anything, possibly by just mapping the wanted memory layout over the memory of the given object.

So, as the "filter" is not the same, using a specific cast is more clear and safe than using the C cast. If you rely on the compiler (or runtime implementation if you use dynamic_cast) to tell you where you did something wrong, by avoid using C cast and reinterepret_cast.

Now that this is more clear, there is another thing: static_cast, reinterpret_cast, const_cast and dynamic_cast are easier to search for.

And the ultimate point: they are ugly. That's wanted. Potentially buggy code, code smells, obvious "tricks" that might generate bugs, are easier to track when it's associated with ugly look. Bad code should be ugly.

That's "by design". And that allows the developer to know where he could have done things better (by totally avoiding casts, if not really needed) and where it's fine but it's "documented" in the code by "marking" it as ugly.

A secondary reason for introducing the new-style cast was that C-style casts are very hard to spot in a program. For example, you can't conveniently search for casts using an ordinary editor or word processor. This near-invisibility of C-style casts is especially unfortunate because they are so potentially damaging. An ugly operation should have an ugly syntactic form. That observation was part of the reason for choosing the syntax for the new-style casts. A further reason was for the new-style casts to match the template notation, so that programmers can write their own casts, especially run-time checked casts.

Maybe, because static_cast is so ugly and so relatively hard to type, you're more likely to think twice before using one? That would be good, because casts really are mostly avoidable in modern C++.

Source: Bjarne Stroustrup (C++ creator)

2 of 9
41

C++ casts are more restrictive (so express your intent better and make code review easier, etc.). They're also much easier to search for, if you ever need to.

🌐
Wikipedia
en.wikipedia.org › wiki › Static_cast
static_cast - Wikipedia
July 30, 2019 - In the C++ programming language, static_cast is an operator that performs an explicit type conversion. ... The type parameter must be a data type to which object can be converted via a known method, whether it be a builtin or a cast. The type can be a reference or an enumerator.
🌐
Educative
educative.io › answers › whats-the-difference-between-staticcast-vs-dynamiccast-in-cpp
What's the difference between static_cast vs dynamic_cast in C++?
static_cast is used for ordinary typecasting. It is responsible for the implicit type of coercion and is also called explicitly. We should use it in cases like converting the int to float, int to char, etc.