The one idea
A cast is a request to treat an expression as having another type, and C++ splits that request into four kinds of permission so that each cast states which rule it is relying on.
static_castperforms conversions the compiler can check from the types alone, including some that are unsafe at run time.const_castadds or removesconstandvolatile, and nothing else.reinterpret_castreuses an address or a bit pattern under another type, and never changes the value representation.dynamic_castconsults the object's dynamic type at run time and can fail.
The C-style cast (T)e and the functional cast T(e) are not a fifth kind. They try the named casts in a fixed order and quietly use the first that is well-formed, which is why they are dangerous.
static_cast: conversions the type system can check
[expr.static.cast] allows four broad groups of conversions.
- Anything direct-initialization could do:
static_cast<T>(e)is valid ifT t(e);would be, and it means the same thing. This includes explicit constructors and explicit conversion functions. - Conversion to
void, which discards a value. - The reverse of many standard conversions: integer or floating to enumeration,
void*toT*, base pointer or reference to derived, and pointer to member of base to pointer to member of derived. It cannot reverse lvalue-to-rvalue, array-to-pointer, function-to-pointer or the boolean conversion, and it never removesconst. - A glvalue to an rvalue reference,
static_cast<T&&>(x), which is whatstd::moveis.
The downcast in group 3 is checked only statically. If a Base* does not actually point to a Base subobject of a Derived, the cast itself has undefined behavior, before anyone dereferences the result. The base must also not be virtual: downcasting through a virtual base needs dynamic_cast.
struct Base {};
struct Derived : Base { int extra = 0; };
constexpr bool bad() {
Base b;
Derived* d = static_cast<Derived*>(&b); // undefined: b is not inside a Derived
(void)d;
return true;
}
// static_assert(bad()); // Clang rejects it: "cannot cast object of dynamic type
// // 'Base' to type 'Derived'"; GCC 16 misses this oneCasting an integer to an enumeration without a fixed underlying type is undefined if the value is outside the enumeration's range, since C++17; the enumerations-and-unions topic has the details. static_cast<void*> and back to the original type is always a round trip.
const_cast and when writing is undefined
const_cast changes only cv-qualification, at any level of a pointer or pointer-to-member type, or on a reference. It can also turn an lvalue into an xvalue with const_cast<T&&>. It cannot change any other part of the type.
Removing const is always allowed; what you do next may not be. [dcl.type.cv] says any attempt to modify a const object during its lifetime is undefined behavior. The question is whether the object itself was created const, not whether the path you reached it through was.
constexpr bool writes_const() {
const int x = 1;
*const_cast<int*>(&x) = 2; // undefined: x is a const object
return true;
}
// static_assert(writes_const()); // rejected: modifying a const object
constexpr bool writes_nonconst() {
int y = 1;
const int& r = y;
const_cast<int&>(r) = 2; // fine: y was never const
return y == 2;
}
static_assert(writes_nonconst());String literals are const arrays, so writing through a const_cast of one is undefined too. The legitimate uses are calling an old API that takes char* but does not write, and implementing a non-const member function in terms of its const twin with std::as_const and a const_cast on the result, where the object is known to be non-const.
reinterpret_cast: same bits, different type
reinterpret_cast exists for conversions the type system cannot check. [expr.reinterpret.cast] lists them exhaustively:
- a pointer to an integer type large enough to hold it, and an integer back to a pointer; a round trip through
std::uintptr_tgives the original pointer; - an object pointer to another object pointer type, defined as
static_cast<cv T*>(static_cast<cv void*>(p)), so the address is unchanged; - a function pointer to another function pointer type, where calling through the wrong type is undefined;
- a conversion between object and function pointers, which is conditionally-supported;
- an lvalue to a reference type,
reinterpret_cast<T&>(x), which means*reinterpret_cast<T*>(&x); - a pointer to member to another pointer to member of the same kind.
It cannot cast away const, it cannot convert between arithmetic values (reinterpret_cast<int>(3.0) is ill-formed), and it is never allowed in a constant expression. Producing the new pointer is usually fine; reading through it is governed by the aliasing rules, which the strict-aliasing-and-undefined-behavior topic covers.
dynamic_cast: asking the object
dynamic_cast<T*>(p) and dynamic_cast<T&>(r) look at the most derived object at run time. An upcast to an accessible, unambiguous base is just the implicit conversion and works on any class. Every other use requires the operand's class to be polymorphic, meaning it has at least one virtual function.
- A downcast succeeds if the operand refers to a public base subobject of exactly one
Tobject inside the most derived object. - Otherwise a cross-cast succeeds if the operand is a public base of the most derived object and that object has a public, unambiguous base
T, even ifTis unrelated to the operand's static type. dynamic_cast<void*>(p)returns a pointer to the most derived object, which can differ fromstatic_cast<void*>(p)under multiple inheritance.- On failure, the pointer form returns a null pointer and the reference form throws
std::bad_cast. A null pointer operand gives a null result.
A private or ambiguous base makes the run-time check fail rather than the program ill-formed, because the check is about the dynamic object, not the static types.
#include <cstdio>
#include <typeinfo>
struct A { virtual ~A() = default; };
struct B { virtual ~B() = default; };
struct C : A, B {};
int main() {
C c;
A* pa = &c;
B* pb = dynamic_cast<B*>(pa); // cross-cast: succeeds
std::printf("%d\n", dynamic_cast<void*>(pb) == static_cast<void*>(&c)); // 1
std::printf("%d\n", static_cast<void*>(pb) == static_cast<void*>(&c)); // 0 on common ABIs
A a;
try { (void)dynamic_cast<C&>(a); }
catch (const std::bad_cast&) { std::puts("bad_cast"); } // printed
}During construction and destruction, dynamic_cast treats the object as having the type of the class whose constructor or destructor is running, so a cast to a more derived class fails there.
C-style and functional casts
[expr.cast] defines (T)e as the first of these that is well-formed:
const_cast<T>(e)static_cast<T>(e)static_castfollowed byconst_castreinterpret_cast<T>(e)reinterpret_castfollowed byconst_cast
The static_cast steps also get one extra power: they may convert to or from a base class that is inaccessible, which no named cast can do. So (Base*)&d performs a correct, address-adjusting derived-to-base conversion even when Base is a private base.
The order hides mistakes. (int*)p with p a const char* cannot be a static_cast, so it silently becomes a reinterpret_cast plus a const_cast. Worse, if the classes involved are incomplete at the point of the cast, [expr.cast] makes it unspecified whether the static_cast or reinterpret_cast interpretation is used, so an include you forgot can turn a pointer-adjusting conversion into a plain reinterpretation of the address.
struct Base { int x = 1; };
struct Other { int y = 2; };
struct Derived : Other, private Base {};
void demo() {
Derived d;
// Base* a = static_cast<Base*>(&d); // ill-formed: Base is inaccessible
Base* b = (Base*)&d; // OK: static_cast semantics, access ignored
const char* s = "hi";
int* p = (int*)s; // reinterpret_cast, then const_cast
(void)b; (void)p;
}The functional notation T(e) with a single expression means exactly (T)e, so std::uintptr_t(p) is a reinterpret_cast. T{e} is different: it is list-initialization, which considers only implicit conversions and rejects narrowing. T() value-initializes, and T(a, b) direct-initializes. C++23 adds auto(x) and auto{x}, which produce a decayed prvalue copy.
std::bit_cast and the value category of a cast
C++20 added std::bit_cast<To>(from) in <bit> for the job people used to do with reinterpret_cast or a union: reading an object's bits as another type. Both types must be trivially copyable and of the same size. The result is a new object, so there is no aliasing problem, and it is constexpr unless a pointer, union, volatile member or reference is involved. If the bits do not form a valid value of To, the behavior is undefined. The bit-manipulation topic covers it in more depth.
#include <bit>
#include <cstdint>
constexpr std::uint32_t one = std::bit_cast<std::uint32_t>(1.0f);
static_assert(one == 0x3f800000); // holds wherever float is IEEE binary32Every cast obeys one rule for the category of its result: an lvalue reference type gives an lvalue, an rvalue reference to an object type gives an xvalue, and any other type gives a prvalue. A prvalue of non-class type is never cv-qualified, so static_cast<const int>(i) has type int; for a class type, static_cast<const S>(s) keeps the const.
#include <type_traits>
#include <utility>
int i = 0;
static_assert(std::is_same_v<decltype(static_cast<const int>(i)), int>);
static_assert(std::is_same_v<decltype((static_cast<int&&>(i))), int&&>); // xvalue
static_assert(std::is_same_v<decltype((const_cast<const int&>(i))), const int&>);What to remember
static_castdoes what direct-initialization could, plus checked reversals of standard conversions; a wrong downcast is undefined at the cast.const_castchanges qualifiers only; writing to an object that was created const is undefined, writing to a non-const object through a const path is fine.reinterpret_castkeeps addresses and bits, cannot removeconst, cannot convert arithmetic values, and is never constant.dynamic_castneeds a polymorphic source except for upcasts, supports cross-casts andvoid*, returns null for pointers and throwsstd::bad_castfor references.- A C-style cast tries const, static, static plus const, reinterpret, reinterpret plus const, and may ignore base-class access.
T(e)equals(T)e;T{e}is list-initialization and rejects narrowing.std::bit_castcopies bits into a new object of the same size and is usually constexpr.- Cast results are lvalues for
T&, xvalues forT&&, prvalues otherwise; non-class prvalues lose cv.