C++ style casts are checked by the compiler. C style casts aren't and can fail at runtime.

Also, C++ style casts can be searched for easily, whereas it's really hard to search for C style casts.

Another big benefit is that the 4 different C++ style casts express the intent of the programmer more clearly.

When writing C++ I'd pretty much always use the C++ ones over the the C style.

Answer from Glen on Stack Overflow
๐ŸŒ
Cprogramming.com
cprogramming.com โ€บ reference โ€บ typecasting โ€บ staticcast.html
Static-Cast Typecast - C/C++ Syntax Reference - Cprogramming.com
How to begin Get the book ยท C tutorial C++ tutorial Game programming Graphics programming Algorithms More tutorials
Top answer
1 of 7
324

C++ style casts are checked by the compiler. C style casts aren't and can fail at runtime.

Also, C++ style casts can be searched for easily, whereas it's really hard to search for C style casts.

Another big benefit is that the 4 different C++ style casts express the intent of the programmer more clearly.

When writing C++ I'd pretty much always use the C++ ones over the the C style.

2 of 7
241

In short:

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

More Explanation:

The static cast performs conversions between compatible types. It is similar to the C-style cast, but is more restrictive. For example, the C-style cast would allow an integer pointer to point to a char.

char c = 10;       // 1 byte
int *p = (int*)&c; // 4 bytes

Since this results in a pointer to a 4-byte type, pointing to 1 byte of allocated memory, writing to this pointer will either cause a run-time error or will overwrite some adjacent memory.

*p = 5; // run-time error: stack corruption

In contrast to the C-style cast, the static cast will allow the compiler to check that the pointer and pointee data types are compatible, which allows the programmer to catch this incorrect pointer assignment during compilation.

int *q = static_cast<int*>(&c); // compile-time error

You can also check this page on more explanation on C++ casts : Click Here

Discussions

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
type casting - c style casts or c++ style casts - Software Engineering Stack Exchange
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... More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
February 22, 2011
static_cast and ( ) - C++ Forum
A C-style cast expression may be evaluated as any one of: a const_cast, a static_cast, a static_cast followed by a const_cast, a reinterpret_cast, a reinterpret_cast followed by a const_cast. These possible C++ casts are examined in the above order; the first one that can be satisfied is selected. More on cplusplus.com
๐ŸŒ cplusplus.com
October 21, 2021
c++ - Why use static_cast (x) instead of (T)x or T(x)? - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I've heard that the static_cast function should be preferred to C-style ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ cpp โ€บ language โ€บ static_cast.html
static_cast conversion - cppreference.com
August 18, 2024 - struct B {}; struct D : B { B b; }; D d; B& br1 = d; B& br2 = d.b; static_cast<D&>(br1); // OK, lvalue denoting the original โ€œdโ€ object static_cast<D&>(br2); // UB: the โ€œbโ€ subobject is not a base class subobject ยท 4) If target-type is the (possibly cv-qualified) void, the conversion has no result. In this case, expression is a discarded-value expression.

C++ style casts are checked by the compiler. C style casts aren't and can fail at runtime.

Also, C++ style casts can be searched for easily, whereas it's really hard to search for C style casts.

Another big benefit is that the 4 different C++ style casts express the intent of the programmer more clearly.

When writing C++ I'd pretty much always use the C++ ones over the the C style.

Answer from Glen on Stack Overflow
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
September 1, 2025 - 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.
Find elsewhere
๐ŸŒ
Medium
hitgal.medium.com โ€บ static-cast-and-c-style-cast-type-e5813e9e80fb
static_cast and C-style cast (type) | by Hitgirl | Medium
March 29, 2024 - static_cast and C-style cast (type) static_cast (voidPtr): static_cast is a safer form of casting in C++. It performs compile-time checks to ensure type safety. It is recommended to use โ€ฆ
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ static_cast-in-c-type-casting-operators
static_cast in C++ | Type Casting operators - GeeksforGeeks
November 23, 2021 - 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 (or implicit ones).
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 124345
static_cast and ( ) - C++ Forum
October 21, 2021 - The thing to note with this example is that floating point variables and integral variables are stored very different in memory. It you were to do a straight binary copy (ie: a reinterpret), bar would not be 5, but would probably be several thousand. So static_cast here does a more intelligent cast, where it determines the integral equivilent of 5 and makes necessary binary adjustments.
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ cpp โ€บ language โ€บ explicit_cast.html
Explicit type conversion - cppreference.com
January 19, 2025 - 1) When the C-style cast is encountered, the compiler attempts to interpret it as the following cast expressions, in this order: ... b) static_cast<type-id ๏ปฟ>(unary-expression ๏ปฟ), with extensions: pointer or reference to a derived class is additionally allowed to be cast to pointer or reference to unambiguous base class (and vice versa) even if the base class is inaccessible (that is, this cast ignores the private inheritance specifier).
๐ŸŒ
IBM
ibm.com โ€บ docs โ€บ en โ€บ i โ€บ 7.4.0
The static_cast operator (C++ only)
December 24, 2020 - We cannot provide a description for this page right now
๐ŸŒ
Quora
quora.com โ€บ C-What-does-static-cast-T-do
C++: What does static cast () do? - Quora
I use static_cast frequently when I need to cast from one primitive data type to another. In my current / latest project, especially in the DBOs(database objects, the models so to speak), I use unsigned int for the data type for any sort of integer mostly to allow a bigger range and b
๐ŸŒ
TTTapa
tttapa.github.io โ€บ Pages โ€บ Programming โ€บ Cpp โ€บ Practices โ€บ c-style-casts.html
Don't use C-style casts
March 4, 2025 - Sensible conversions are allowed, for example, casting an integer to a wider integer, or explicitly converting an integer to milliseconds: ... When you need to force a narrowing conversion, use static_cast<type>(expression).
๐ŸŒ
Medium
madhawapolkotuwa.medium.com โ€บ understanding-static-and-dynamic-casting-in-c-b73cf3f92ec2
Understanding Static and Dynamic Casting in C++ | by Madhawa Polkotuwa | Medium
January 22, 2025 - Static casting converts basePtr (a Base*) to a Derived*. The programmer assumes responsibility for ensuring the cast is valid. Since basePtr actually points to a Derived object, the method call works as expected.
๐ŸŒ
Doulos
doulos.com โ€บ knowhow โ€บ arm-embedded โ€บ cplusplus-casting
Introduction to C++ Casting Issues
If the compiler cannot resolve the type conversion as valid, it won't compile. The T is a type name such as int, a class name, or a struct name. static_cast<T> does things like implicit conversions between compatible types (such as int to float or pointer to void*), and it can also use explicit conversion functions (or implicit ones).
๐ŸŒ
StudyPlan.dev
studyplan.dev โ€บ intro-to-programming โ€บ casting
Intro to C++: Static Casting Explained Simply | A Practical Guide
March 1, 2025 - The equivalent C-style cast performs ... 6} ... Static casting is a type of explicit conversion performed at build time, allowing for type conversion with minimal or no performance impact....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ static-cast-in-cplusplus
static_cast in C++
December 26, 2022 - The static_cast is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coercion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc. This can ca
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.

Top answer
1 of 8
1887

static_cast

static_cast is used for cases where you basically want to reverse an implicit conversion, with a few restrictions and additions. static_cast performs no runtime checks. This should be used if you know that you refer to an object of a specific type, and thus a check would be unnecessary. Example:

void func(void *data) {
  // Conversion from MyClass* -> void* is implicit
  MyClass *c = static_cast<MyClass*>(data);
  ...
}

int main() {
  MyClass c;
  start_thread(&func, &c)  // func(&c) will be called
      .join();
}

In this example, you know that you passed a MyClass object, and thus there isn't any need for a runtime check to ensure this.

dynamic_cast

dynamic_cast is useful when you don't know what the dynamic type of the object is. It returns a null pointer if the object referred to doesn't contain the type casted to as a base class (when you cast to a reference, a bad_cast exception is thrown in that case).

if (JumpStm *j = dynamic_cast<JumpStm*>(&stm)) {
  ...
} else if (ExprStm *e = dynamic_cast<ExprStm*>(&stm)) {
  ...
}

You can not use dynamic_cast for downcast (casting to a derived class) if the argument type is not polymorphic. For example, the following code is not valid, because Base doesn't contain any virtual function:

struct Base { };
struct Derived : Base { };
int main() {
  Derived d; Base *b = &d;
  dynamic_cast<Derived*>(b); // Invalid
}

An "up-cast" (cast to the base class) is always valid with both static_cast and dynamic_cast, and also without any cast, as an "up-cast" is an implicit conversion (assuming the base class is accessible, i.e. it's a public inheritance).

Regular Cast

These casts are also called C-style cast. A C-style cast is basically identical to trying out a range of sequences of C++ casts, and taking the first C++ cast that works, without ever considering dynamic_cast. Needless to say, this is much more powerful as it combines all of const_cast, static_cast and reinterpret_cast, but it's also unsafe, because it does not use dynamic_cast.

In addition, C-style casts not only allow you to do this, but they also allow you to safely cast to a private base-class, while the "equivalent" static_cast sequence would give you a compile-time error for that.

Some people prefer C-style casts because of their brevity. I use them for numeric casts only, and use the appropriate C++ casts when user defined types are involved, as they provide stricter checking.

2 of 8
303

Static cast

The static cast performs conversions between compatible types. It is similar to the C-style cast, but is more restrictive. For example, the C-style cast would allow an integer pointer to point to a char.
char c = 10;       // 1 byte
int *p = (int*)&c; // 4 bytes

Since this results in a 4-byte pointer pointing to 1 byte of allocated memory, writing to this pointer will either cause a run-time error or will overwrite some adjacent memory.

*p = 5; // run-time error: stack corruption

In contrast to the C-style cast, the static cast will allow the compiler to check that the pointer and pointee data types are compatible, which allows the programmer to catch this incorrect pointer assignment during compilation.

int *q = static_cast<int*>(&c); // compile-time error

Reinterpret cast

To force the pointer conversion, in the same way as the C-style cast does in the background, the reinterpret cast would be used instead.

int *r = reinterpret_cast<int*>(&c); // forced conversion

This cast handles conversions between certain unrelated types, such as from one pointer type to another incompatible pointer type. It will simply perform a binary copy of the data without altering the underlying bit pattern. Note that the result of such a low-level operation is system-specific and therefore not portable. It should be used with caution if it cannot be avoided altogether.

Dynamic cast

This one is only used to convert object pointers and object references into other pointer or reference types in the inheritance hierarchy. It is the only cast that makes sure that the object pointed to can be converted, by performing a run-time check that the pointer refers to a complete object of the destination type. For this run-time check to be possible the object must be polymorphic. That is, the class must define or inherit at least one virtual function. This is because the compiler will only generate the needed run-time type information for such objects.

Dynamic cast examples

In the example below, a MyChild pointer is converted into a MyBase pointer using a dynamic cast. This derived-to-base conversion succeeds, because the Child object includes a complete Base object.

class MyBase 
{ 
  public:
  virtual void test() {}
};
class MyChild : public MyBase {};



int main()
{
  MyChild *child = new MyChild();
  MyBase  *base = dynamic_cast<MyBase*>(child); // ok
}

The next example attempts to convert a MyBase pointer to a MyChild pointer. Since the Base object does not contain a complete Child object this pointer conversion will fail. To indicate this, the dynamic cast returns a null pointer. This gives a convenient way to check whether or not a conversion has succeeded during run-time.

MyBase  *base = new MyBase();
MyChild *child = dynamic_cast<MyChild*>(base);

 
if (child == 0) 
std::cout << "Null pointer returned";

If a reference is converted instead of a pointer, the dynamic cast will then fail by throwing a bad_cast exception. This needs to be handled using a try-catch statement.

#include <exception>
// โ€ฆ  
try
{ 
  MyChild &child = dynamic_cast<MyChild&>(*base);
}
catch(std::bad_cast &e) 
{ 
  std::cout << e.what(); // bad dynamic_cast
}

Dynamic or static cast

The advantage of using a dynamic cast is that it allows the programmer to check whether or not a conversion has succeeded during run-time. The disadvantage is that there is a performance overhead associated with doing this check. For this reason using a static cast would have been preferable in the first example, because a derived-to-base conversion will never fail.

MyBase *base = static_cast<MyBase*>(child); // ok

However, in the second example the conversion may lead to run-time error. The error will happen if the MyBase object contains a MyBase instance but not if it contains a MyChild instance. In some situations this may not be known until run-time. When this is the case dynamic cast is a better choice than static cast.

// Succeeds for a MyChild object
MyChild *child = dynamic_cast<MyChild*>(base);

If the base-to-derived conversion had been performed using a static cast instead of a dynamic cast the conversion would not have failed. It would have returned a pointer that referred to an incomplete object. Dereferencing such a pointer can lead to run-time errors.

// Allowed, but invalid
MyChild *child = static_cast<MyChild*>(base);
 
// Incomplete MyChild object dereferenced
(*child);

Const cast

This one is primarily used to add or remove the const modifier of a variable.

const int myConst = 5;
int *nonConst = const_cast<int*>(&myConst); // removes const

Although const cast allows the value of a constant to be changed, doing so is still invalid code that may cause a run-time error. This could occur for example if the constant was located in a section of read-only memory.

*nonConst = 10; // potential run-time error

const cast is instead used mainly when there is a function that takes a non-constant pointer argument, even though it does not modify the pointee.

void print(int *p) 
{
   std::cout << *p;
}

The function can then be passed a constant variable by using a const cast.

print(&myConst); // error: cannot convert 
                 // const int* to int*
 
print(nonConst); // allowed

Source and More Explanations

๐ŸŒ
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?
February 2, 2026 - In C like cast sometimes we can cast some type pointer to point some other type data. Like one integer pointer can also point character type data, as they are quite similar, only difference is character has 1-byte, integer has 4-bytes. In C++ the static_cast() is more strict than C like casting.