Casts
A cast is an explicit conversion. C++ has four named casts — static_cast, dynamic_cast, const_cast, reinterpret_cast — plus the function-style and C-style casts you should not write in new code. Each named cast says why the conversion is allowed.
Detailed Explanation
Implicit conversions already cover the safe everyday cases: int to double, derived pointer to base pointer, T* to void*. A cast is for the conversions the language will not do silently, or for the ones you want a reviewer to see.
static_cast<T>(expr) is the workhorse. It performs well-defined conversions the compiler can check: numeric conversions, void* back to T* if you know the type, explicit constructors, downcasts in a hierarchy when you are sure of the dynamic type, enum to integer and back. It will not cast away const. It will not turn an int* into a double* . If static_cast refuses, the conversion is either unsafe or needs a different cast.
dynamic_cast<T>(expr) is the runtime-checked cast in a polymorphic hierarchy (a class with at least one virtual function). On pointers, a failed downcast returns nullptr. On references, a failed downcast throws std::bad_cast. It needs RTTI (-fno-rtti disables it). Use it when you genuinely do not know the dynamic type and have no better API (a virtual function, a visitor). A design that needs dynamic_cast on every call is usually a missing virtual method.
const_cast<T>(expr) adds or removes const or volatile. Removing const and then writing to an object that was originally const is undefined behaviour. The only honest uses are talking to a C API that forgot const on a read-only parameter, or implementing a const method that delegates to a non-const one and then re-adds const on the result.
reinterpret_cast<T>(expr) reinterprets the bits as a different pointer or integer type. It is how you turn a pointer into std::uintptr_t, or a void* from mmap back into a Header*. The result is not safe to dereference unless the alignment and type match the object's actual type (or you are inspecting bytes via unsigned char / std::byte). Most "I need reinterpret_cast" moments are a missing static_cast or a memcpy of bytes.
C-style (T)expr and function-style T(expr) can mean any of the above, including reinterpret_cast and const_cast, depending on what compiles. That is why they are banned in style guides. A named cast documents intent and greps cleanly.
std::bit_cast<T>(value) (C++20) copies the bytes of a trivially copyable value into another trivially copyable type of the same size. It is the defined way to view a float as a uint32_t. Prefer it over a reinterpret_cast on a pointer to the float.
Numeric narrowing (double to int) is a static_cast you should write on purpose. Implicit narrowing in list initialisation is a compile error: int n{3.1}; fails. int n = 3.1; silently truncates. Be explicit.
Code Examples
#include <cstdint>
#include <iostream>
enum class Port : std::uint16_t { Http = 80 };
int main() {
double price = 19.99;
int cents = static_cast<int>(price * 100); // 1999, truncation is visible
auto raw = static_cast<std::uint16_t>(Port::Http);
std::cout << cents << ' ' << raw << '\n'; // prints: 1999 80
}#include <iostream>
#include <memory>
struct Shape { virtual ~Shape() = default; };
struct Circle : Shape { int r = 3; };
int main() {
std::unique_ptr<Shape> s = std::make_unique<Circle>();
if (auto* c = dynamic_cast<Circle*>(s.get())) {
std::cout << c->r << '\n'; // prints: 3
}
}Common Gotchas
Writing through `const_cast` on a const object
`const int n = 1; *const_cast<int*>(&n) = 2;` is undefined behaviour. The cast compiles. The write is the bug.
C-style casts hide `reinterpret_cast`
`(Widget*)p` can compile when `static_cast<Widget*>(p)` would not. Use the named cast so a mistake is a compiler error.
`dynamic_cast` requires a polymorphic type
without a virtual function, `dynamic_cast` to a sibling or down the tree does not compile (or does not do what you think on some compilers). Add a virtual destructor to bases you delete through.
`static_cast` down the tree is unchecked
it is faster than `dynamic_cast` and wrong if the object is not that type. Use it only when the type is already known from context.