Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

static_cast is a compile-time postfix operator in C++ used to perform explicit type conversions between related types. It relies exclusively on static type information provided at compile time to determine the validity of the conversion and the necessary memory layout adjustments, performing absolutely no runtime type checking (RTTI).

Syntax

static_cast<new_type>(expression)
  • new_type: The target type to which the expression is being converted.
  • expression: The value or object being evaluated and converted.
  • Return Value: Returns a value of type new_type. If new_type is an lvalue reference or an rvalue reference to a function, the result is an lvalue; if it is an rvalue reference to an object, the result is an xvalue; otherwise, it is a prvalue.

Mechanical Behavior

When the compiler encounters a static_cast, it determines the validity of the conversion by first checking if direct initialization is well-formed (conceptually evaluating new_type t(expression);).
  1. Direct Initialization: During this step, the compiler considers both standard conversion sequences (e.g., integral promotions, floating-point truncations) and user-defined conversions (e.g., constructors, conversion operators) simultaneously. It applies standard C++ overload resolution rules to find the single best conversion path.
  2. Inverse Conversions: If direct initialization is not well-formed, static_cast checks if the requested conversion is the exact reverse of a standard implicit conversion (such as downcasting a pointer or converting void* to a typed pointer). If such an inverse relationship exists and does not violate cv-qualification rules, the cast is permitted.

Permitted Conversions

The compiler strictly limits static_cast to the following structural relationships:
  • Numeric Conversions: Converts between fundamental numeric types (e.g., float to int, int to char). This includes both value-preserving promotions and lossy truncations.
  • Class Hierarchy Navigation (Upcasting): Converts a pointer or reference of a derived class to a pointer or reference of an unambiguous, accessible base class. This is identical to implicit upcasting.
  • Class Hierarchy Navigation (Downcasting): Converts a pointer or reference of a base class to a pointer or reference of a derived class. Crucial mechanical note: Because static_cast does not inspect the runtime type, if the object being pointed to or referenced is not actually a valid subobject of the target derived type, the cast itself evaluates to Undefined Behavior (UB), regardless of whether the resulting pointer or reference is ever dereferenced.
  • void* Conversions: Converts a pointer to an object type to a void* (type erasure) and converts a void* to any pointer-to-object type. Provided alignment requirements are met, the cast is legal, though dereferencing the resulting pointer may violate strict aliasing rules if it does not match the object’s dynamic type.
  • Enumeration Conversions: Converts an integer to an enumeration type (scoped or unscoped) and vice versa. If the enumeration does not have a fixed underlying type, casting a value outside the range of the enumeration values results in Undefined Behavior (UB). If the enumeration does have a fixed underlying type, the conversion is well-defined.
  • Lvalue to Rvalue Casts: Casts an lvalue to an rvalue reference (static_cast<T&&>(val)), which is the underlying mechanical implementation of std::move.
  • Discarded-Value Expressions: Converts any expression to void (e.g., static_cast<void>(expression)). This explicitly evaluates and discards the result of the expression, which is the standard mechanism for suppressing compiler warnings regarding unused variables or ignored [[nodiscard]] return values.

Strict Restrictions

The compiler will reject a static_cast if any of the following conditions apply:
  • CV-Qualifier Modification: It cannot cast away const or volatile qualifiers. Attempting to do so results in a compilation error (this requires const_cast).
  • Unrelated Pointer Types: It cannot convert between pointers of fundamentally unrelated types (e.g., int* to float*, or BaseA* to BaseB* where neither inherits from the other). This requires reinterpret_cast.
  • Virtual Inheritance Downcasting: It cannot perform a downcast from a virtual base class to a derived class. The memory offset of a virtual base is not known at compile time, making static resolution impossible (this requires dynamic_cast).
Master C++ with Deep Grasping Methodology!Learn More