Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The C++ cast operators

Reference for the four C++ cast operators: static_cast, dynamic_cast, const_cast and reinterpret_cast, what each allows, failure behavior, bit_cast and why to avoid C-style casts.

C++ has four named casts, each with a narrow job, so that a reader (and the compiler) can see exactly what kind of conversion you intend. They replace the C-style cast (T)x, which can silently do any of them, in the worst case a combination.

static_cast performs well-defined conversions checked at compile time. dynamic_cast navigates polymorphic class hierarchies at run time and can fail safely. const_cast adds or removes const/volatile. reinterpret_cast reinterprets bits and is the most dangerous. Most good code needs mostly the first, occasionally the second, and rarely the other two.

C++
double ratio = static_cast<double>(a) / b;      // numeric conversion
auto* d = dynamic_cast<Dog*>(animalPtr);        // nullptr if not a Dog

Try it

C++ static_cast and dynamic_cast

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ const_cast, reinterpret_cast and std::bit_cast

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
static_cast<T>(expr)
dynamic_cast<T>(expr)       // T: pointer or reference to a class, or void*
const_cast<T>(expr)
reinterpret_cast<T>(expr)
Cast What it does Failure
static_cast Related types: numeric conversions, base/derived pointers (downcast unchecked), void* to T*, enum/int, explicit constructors Compile error if no conversion exists; wrong downcast is undefined behavior
dynamic_cast Checked up/down/sideways casts in polymorphic classes (needs a virtual function) Pointer: nullptr. Reference: throws std::bad_cast
const_cast Adds/removes const and volatile on pointers, references, member pointers Writing to an object that is really const is undefined behavior
reinterpret_cast Pointer/integer and unrelated pointer type reinterpretation Nothing checked; wrong use is undefined behavior

Usage notes

  • Prefer no cast. Use static_cast for intentional narrowing so a reader sees it, and use brace initialization {} to detect accidental narrowing.
  • dynamic_cast needs RTTI and a polymorphic type; it costs a table lookup. A design that needs many downcasts often wants a virtual function or std::variant instead.
  • Use static_cast<Derived*>(basePtr) only when you know the dynamic type; otherwise it is undefined behavior.
  • Casting to void* with dynamic_cast gives a pointer to the most-derived object.
  • const_cast is for calling old APIs that are not const-correct, or for the "call the const overload and strip const" trick. Never use it to modify an object defined const.
  • reinterpret_cast gives unspecified results in most uses; type punning through it violates strict aliasing. Use std::memcpy or std::bit_cast (C++20) to read an object's bits. Access through char, unsigned char and std::byte is the allowed exception.
  • A C-style cast (T)x or functional cast T(x) tries const_cast, static_cast and reinterpret_cast in turn, so it hides which one you got. Avoid it in new code.
  • static_cast<T&&>(x) is what std::move and std::forward do internally.