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.
double ratio = static_cast<double>(a) / b; // numeric conversion auto* d = dynamic_cast<Dog*>(animalPtr); // nullptr if not a Dog
Try it
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.
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
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_castfor intentional narrowing so a reader sees it, and use brace initialization{}to detect accidental narrowing. dynamic_castneeds RTTI and a polymorphic type; it costs a table lookup. A design that needs many downcasts often wants a virtual function orstd::variantinstead.- Use
static_cast<Derived*>(basePtr)only when you know the dynamic type; otherwise it is undefined behavior. - Casting to
void*withdynamic_castgives a pointer to the most-derived object. const_castis 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 definedconst.reinterpret_castgives unspecified results in most uses; type punning through it violates strict aliasing. Usestd::memcpyorstd::bit_cast(C++20) to read an object's bits. Access throughchar,unsigned charandstd::byteis the allowed exception.- A C-style cast
(T)xor functional castT(x)triesconst_cast,static_castandreinterpret_castin turn, so it hides which one you got. Avoid it in new code. static_cast<T&&>(x)is whatstd::moveandstd::forwarddo internally.
Related
- typeid
- Type conversions
- const
- virtual
- Tutorial: RTTI and casts, Type conversions