static_cast and reinterpret_cast are the two C++ cast operators that separate compile-time value or hierarchy conversions from low-level pointer and bit reinterpretation. Instead of combining value conversion, pointer reinterpretation, runtime polymorphic checks (dynamic_cast), and qualifier removal (const_cast) into a single syntax, C++ gives each operation its own named cast. Using real code and compiler outputs verified on 2026-10-28 (KST) with g++ (Debian 14.2.0-19) 14.2.0 on x86_64, this post explains when to use each cast and which conversions the compiler rejects.

Key Takeaway

Use static_cast for conversions the compiler understands—numeric value conversions, scoped enum to integer, restoring void* back to its original object pointer type, and pointer or reference conversions within an inheritance hierarchy. Use reinterpret_cast for low-level conversions that reinterpret the underlying bits or change how a pointer is viewed—converting between a pointer and std::uintptr_t, or between unrelated pointer types. As verified by the compiler errors below, static_cast cannot convert int* to double*, while reinterpret_cast cannot convert a double value to int and cannot remove const. For runtime polymorphic type checking use dynamic_cast, and to cast away const use const_cast.

Diagram comparing static_cast for compiler-known conversions and reinterpret_cast for pointer reinterpretation and address–integer conversion

Between static_cast and reinterpret_cast, when do you use static_cast?

One-line answer: Use static_cast for conversions whose rules the compiler knows—numeric value conversions, scoped enum to integer, restoring void* to the original object pointer type, and base/derived conversions along an inheritance hierarchy.

According to the cppreference documentation on static_cast, it handles numeric conversions (such as truncating a floating-point value to an integer), conversions between a scoped enumeration (enum class) and an integer, converting void* back to the original object pointer type, and converting pointers or references between base and derived classes within an inheritance hierarchy.

However, a static_cast downcast from a base pointer to a derived pointer performs no runtime check; if the pointed-to object is not actually of the derived type, the behavior is undefined. By contrast, dynamic_cast on polymorphic types checks the dynamic type at runtime and returns nullptr when a pointer downcast fails. For why polymorphic base classes define a virtual destructor (virtual ~Base() = default;), see What is a virtual destructor in C++? Why deleting through a base pointer requires virtual.

Here is casts_ok.cpp, which demonstrates static_cast, dynamic_cast, and reinterpret_cast together (the inline comments in the source file explain each conversion):

#include <cstdint>
#include <cstdio>

struct Base { virtual ~Base() = default; };
struct Derived : Base { int v = 7; };

int main() {
    // static_cast: 값 변환 (double -> int, 소수점 버림)
    double d = 3.99;
    int i = static_cast<int>(d);
    std::printf("static_cast<int>(3.99) = %d\n", i);

    // static_cast: enum class -> int
    enum class Color { Red = 1, Green = 2 };
    std::printf("static_cast<int>(Color::Green) = %d\n", static_cast<int>(Color::Green));

    // static_cast: void* -> 원래 타입 포인터로 되돌리기
    int n = 10;
    void* vp = &n;
    int* ip = static_cast<int*>(vp);
    std::printf("static_cast<int*>(void*) -> *ip = %d\n", *ip);

    // static_cast: 업캐스트/다운캐스트 (다운캐스트는 실제 타입을 확인하지 않음)
    Derived der;
    Base* bp = &der;                              // 업캐스트는 암시적
    Derived* dp = static_cast<Derived*>(bp);      // 실제 Derived라서 OK
    std::printf("static_cast<Derived*>(Base*) -> v = %d\n", dp->v);
    Base plain;
    Derived* bad = dynamic_cast<Derived*>(&plain); // 실제 타입 확인 -> nullptr
    std::printf("dynamic_cast on real Base -> %s\n", bad ? "non-null" : "nullptr");

    // reinterpret_cast: 포인터 <-> 정수 (주소 값을 정수로)
    std::uintptr_t addr = reinterpret_cast<std::uintptr_t>(&n);
    int* back = reinterpret_cast<int*>(addr);
    std::printf("reinterpret_cast round trip same pointer: %s, *back = %d\n",
                back == &n ? "true" : "false", *back);
}

Compiling with $ g++ -std=c++17 -Wall -Wextra casts_ok.cpp -o casts_ok succeeds (build exit=0) and emits one warning:

casts_ok.cpp: In function ‘int main()’:
casts_ok.cpp:29:20: warning: ‘dynamic_cast<struct Derived*>(Base plain)’ can never succeed
   29 |     Derived* bad = dynamic_cast<Derived*>(&plain); // 실제 타입 확인 -> nullptr
      |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Because the compiler can see directly that plain is a real Base object rather than a Derived object, it warns that the dynamic_cast can never succeed. Running $ ./casts_ok prints:

static_cast<int>(3.99) = 3
static_cast<int>(Color::Green) = 2
static_cast<int*>(void*) -> *ip = 10
static_cast<Derived*>(Base*) -> v = 7
dynamic_cast on real Base -> nullptr
reinterpret_cast round trip same pointer: true, *back = 10

The first five lines show floating-point truncation to int (3), scoped enum to int (2), restoring void* back to int* (10), downcasting a Base* that actually points to Derived (7), and dynamic_cast returning nullptr on a real Base instance.

When do you use reinterpret_cast?

One-line answer: Use reinterpret_cast when converting between a pointer and an integer large enough to hold it (std::uintptr_t) or when viewing a pointer as an unrelated pointer type.

According to cppreference, reinterpret_cast reinterprets the underlying bit pattern or changes how a pointer is viewed. Converting a pointer to an integer type large enough to hold it (std::uintptr_t) and converting it back to the original pointer type is guaranteed to yield the original pointer value, as shown in the final output line of casts_ok (reinterpret_cast round trip same pointer: true, *back = 10).

In embedded programming, converting a fixed hardware register address to a pointer is a typical use of reinterpret_cast.

For the concept of byte order (big-endian vs. little-endian) in memory, see What is endianness? Distinguishing big-endian and little-endian.

Which conversions can each cast not perform?

One-line answer: static_cast cannot convert between unrelated pointer types (such as int* to double*), while reinterpret_cast cannot perform value conversions (such as double to int) or cast away const.

Compiling each of the three invalid conversions below with g++ -std=c++17 -Wall -Wextra -c fails with exit status 1 (exit=1).

First, in casts_err1.cpp (with int n = 1;), attempting to convert &n to double* using static_cast fails ($ g++ -std=c++17 -Wall -Wextra -c casts_err1.cpp):

casts_err1.cpp: In function ‘int main()’:
casts_err1.cpp:3:17: error: invalid ‘static_cast’ from type ‘int*’ to type ‘double*’
    3 |     double* p = static_cast<double*>(&n);  // 관련 없는 포인터 타입끼리: static_cast 불가
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~

Second, in casts_err2.cpp (with double d = 3.5;), attempting a numeric value conversion to int using reinterpret_cast fails ($ g++ -std=c++17 -Wall -Wextra -c casts_err2.cpp):

casts_err2.cpp: In function ‘int main()’:
casts_err2.cpp:3:13: error: invalid cast from type ‘double’ to type ‘int’
    3 |     int i = reinterpret_cast<int>(d);      // 값 변환은 reinterpret_cast로 못 함
      |             ^~~~~~~~~~~~~~~~~~~~~~~~

Third, in casts_err3.cpp (with const int c = 5;), attempting to remove const using reinterpret_cast fails because only const_cast can cast away qualifiers ($ g++ -std=c++17 -Wall -Wextra -c casts_err3.cpp):

casts_err3.cpp: In function ‘int main()’:
casts_err3.cpp:3:14: error: ‘reinterpret_cast’ from type ‘const int*’ to type ‘int*’ casts away qualifiers
    3 |     int* p = reinterpret_cast<int*>(&c);   // const 제거는 const_cast만 가능
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~~

The table below summarizes which conversions each C++ cast supports based on the cppreference specifications and the compiler checks above:

Conversion kindstatic_castreinterpret_castconst_castdynamic_cast
Numeric value conversion (double → int)AllowedNot allowedNot allowedNot allowed
Scoped enum (enum class) ↔ integer (int)AllowedNot allowedNot allowedNot allowed
void* → original object pointer (T*)AllowedAllowedNot allowedNot allowed
Inheritance hierarchy pointers (Base* ↔ Derived*)Allowed (no runtime check)Allowed (not recommended)Not allowedRuntime check (polymorphic types)
Unrelated pointer types (int* → double*)Not allowedAllowedNot allowedNot allowed
Pointer ↔ integer (std::uintptr_t)Not allowedAllowedNot allowedNot allowed
Removing const qualifierNot allowedNot allowedAllowedNot allowed

Guide table for choosing static_cast, reinterpret_cast, const_cast, and dynamic_cast by conversion kind

Why should you avoid the C-style cast (T)x?

One-line answer: The compiler resolves (T)x by trying const_cast, static_cast, reinterpret_cast, and their combinations in order and picking the first one that works, so the code hides which conversion actually happened.

According to the cppreference documentation on explicit type conversion, a C-style cast written as (T)x or T(x) causes the compiler to attempt the following casts in order and use the first one that succeeds:

  1. const_cast<T>(x)
  2. static_cast<T>(x)
  3. static_cast<T>(x) followed by const_cast
  4. reinterpret_cast<T>(x)
  5. reinterpret_cast<T>(x) followed by const_cast

Because of this fallback order, a C-style cast can silently turn into a reinterpret_cast or strip const when types change, instead of producing a compile error. For preventing unintended implicit conversions through class constructors, see What is explicit in C++? When you need to prevent implicit constructor conversions.

What are the frequently asked questions?

One-line answer: Here are answers regarding strict aliasing when dereferencing a pointer from reinterpret_cast, C++20 std::bit_cast, and how explicit casts relate to integer promotion.

Is it safe to dereference a pointer obtained with reinterpret_cast?

Accessing an object through a pointer of an unrelated type obtained with reinterpret_cast can violate the type-aliasing (strict aliasing) rule and result in undefined behavior.

What is std::bit_cast?

Introduced in C++20 (<bit>), std::bit_cast copies the object representation of a value into a different type of the same size, providing the defined way to reinterpret the bits of a value.

How does this relate to integer promotion?

For how small integer types such as bool, char, and short are implicitly promoted in expressions before any explicit cast, see What is C++ integral promotion? Why ~true evaluates to true.

One-line answer: These related posts cover C++ integral promotion, preventing implicit constructor conversions, virtual destructors, endianness, and memory segments.

What are the sources?

One-line answer: The sources are the cppreference pages for static_cast, reinterpret_cast, and explicit type conversion, together with our GCC verification.