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.

reinterpret_cast is a C++ casting operator that performs low-level, bitwise reinterpretation of an expression’s underlying memory pattern into a different type. It bypasses the C++ type system’s safety checks, instructing the compiler to treat a specific sequence of bits as if it were of the specified target type without altering the actual bit values or performing any runtime conversion overhead.

Syntax

reinterpret_cast<new_type>(expression)
  • new_type: Must be a pointer, reference, integral type, pointer-to-function, or pointer-to-member type.
  • expression: The value whose bit pattern is being reinterpreted.

Core Mechanics

Unlike static_cast or dynamic_cast, reinterpret_cast does not adjust memory addresses. In scenarios involving multiple inheritance, static_cast will apply the necessary offset to a pointer to point to the correct base class sub-object. reinterpret_cast will not; it strictly returns the exact same memory address typed as the new pointer. At the machine level, reinterpret_cast typically produces no executable instructions. It is purely a compile-time directive that alters the abstract syntax tree’s type information for the given expression.

Permitted Conversions

The C++ standard restricts reinterpret_cast to specific categories of type conversions:
  1. Pointer to Pointer: Any pointer to an object or incomplete type can be cast to any other pointer type.
  2. Reference to Reference: An lvalue expression can be cast to a reference of another type. The result is an lvalue or xvalue referring to the same object.
  3. Pointer to Integer: A pointer can be cast to an integral type. The integral type must be wide enough to hold the pointer’s value (e.g., std::uintptr_t or std::intptr_t).
  4. Integer or Enumeration to Pointer: A value of integral or enumeration type can be cast to a pointer type. Casting a pointer to an integer of sufficient size and back to the original pointer type guarantees the original value is restored.
  5. Function Pointer to Function Pointer: A pointer to a function can be cast to a pointer to a different function type. Calling the function through the reinterpreted pointer without casting it back to its original type results in undefined behavior.
  6. Pointer-to-Member to Pointer-to-Member: A prvalue of type pointer-to-member of one class can be cast to a pointer-to-member of another class.
#include <cstdint>

struct ClassA { int x; };
struct ClassB { double y; };
enum class AddressEnum : std::uintptr_t { Base = 0x1000 };

int source_val = 42;
int* ptr = &source_val;

// Pointer to Pointer
double* d_ptr = reinterpret_cast<double*>(ptr);

// Reference to Reference
double& d_ref = reinterpret_cast<double&>(source_val);

// Pointer to Integer
std::uintptr_t address_val = reinterpret_cast<std::uintptr_t>(ptr);

// Integer/Enumeration to Pointer
int* restored_ptr = reinterpret_cast<int*>(address_val);
int* enum_ptr = reinterpret_cast<int*>(AddressEnum::Base);

// Pointer-to-Member to Pointer-to-Member
int ClassA::* p_member_A = &ClassA::x;
int ClassB::* p_member_B = reinterpret_cast<int ClassB::*>(p_member_A);

Technical Restrictions

  • Const Correctness: reinterpret_cast cannot cast away const, volatile, or __unaligned (a Microsoft-specific MSVC compiler extension) qualifiers. Attempting to do so results in a compilation error. Removing these qualifiers strictly requires const_cast.
  • Value Casting: It cannot convert between non-pointer and non-reference fundamental types directly (e.g., casting a float value to an int value).
  • Strict Aliasing Rule: While reinterpret_cast allows the creation of a pointer or reference of a different type, dereferencing that pointer or reference violates the strict aliasing rule and invokes undefined behavior, unless the target type is:
    • Similar to the dynamic type of the object.
    • The signed or unsigned variant of the dynamic type.
    • char, unsigned char, or std::byte.
float f = 3.14f;

// ILLEGAL: Cannot cast values directly
// int i = reinterpret_cast<int>(f); 

// LEGAL CAST, BUT ILLEGAL DEREFERENCE (Strict Aliasing Violation / Undefined Behavior)
int* p_int = reinterpret_cast<int*>(&f);
// int val = *p_int; 

// LEGAL CAST AND LEGAL DEREFERENCE (Byte-level inspection is permitted by the standard)
unsigned char* p_bytes = reinterpret_cast<unsigned char*>(&f);
unsigned char first_byte = p_bytes[0]; 
Master C++ with Deep Grasping Methodology!Learn More