> ## 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.

# C++ Functional Cast

Standard C++ does not possess a `type(...)` operator. Type querying and deduction are instead handled by two distinct standard operators: `decltype(...)` for compile-time type deduction, and `typeid(...)` for Run-Time Type Information (RTTI) and static type identification.

## `decltype(...)`

The `decltype` operator inspects the declared type of an entity or the type and value category of an expression. It is evaluated entirely at compile-time and does not execute the expression it evaluates.

**Syntax:**

```cpp theme={"dark"}
decltype(entity)
decltype(expression)
```

**Evaluation Rules:**

1. **Unparenthesized id-expression or class member access:** If the operand is an unparenthesized identifier or a member access expression (`obj.member` or `ptr->member`), `decltype` yields the exact declared type of the entity.
2. **Parenthesized expression or other expressions:** If the operand is an expression of type `T`, the resulting type depends on the expression's value category:
   * If the expression is an **lvalue**, `decltype` yields `T&`.
   * If the expression is an **xvalue**, `decltype` yields `T&&`.
   * If the expression is a **prvalue**, `decltype` yields `T`.

**Mechanics Example:**

```cpp theme={"dark"}
#include <utility>

int x = 0;
const int cx = 0;
int& rx = x;
struct S { int m; };
S s;

using T1 = decltype(x);            // Yields: int
using T2 = decltype(cx);           // Yields: const int
using T3 = decltype(rx);           // Yields: int&
using T4 = decltype(s.m);          // Yields: int (unparenthesized member access)
using T5 = decltype((s.m));        // Yields: int& (parenthesized lvalue expression)
using T6 = decltype(x + 1);        // Yields: int (prvalue expression)
using T7 = decltype(std::move(x)); // Yields: int&& (xvalue expression)
```

## `typeid(...)`

The `typeid` operator queries information about a type. The operator yields an lvalue of type `const std::type_info` (defined in the `<typeinfo>` header) that represents the type.

**Syntax:**

```cpp theme={"dark"}
typeid(type)
typeid(expression)
```

**Evaluation Rules:**

1. **Type Operand:** If the operand is a type-id, `typeid` evaluates at compile-time and yields the `const std::type_info` for that exact type.
2. **Non-Polymorphic Expression Operand:** If the operand is an expression whose static type is not a polymorphic class (a class with at least one virtual function), `typeid` evaluates at compile-time. The expression is not executed.
3. **Polymorphic Expression Operand:** If the operand is a glvalue expression of a polymorphic class type, `typeid` evaluates at run-time. It inspects the vtable to yield the `const std::type_info` of the most derived (dynamic) type of the object.
4. **Null Pointer Dereference:** If the operand is a dereferenced null pointer of a polymorphic class type (e.g., `typeid(*ptr)` where `ptr` evaluates to a null pointer value), the operator throws a `std::bad_typeid` exception.
5. **CV-Qualifiers and References:** `typeid` strips top-level `const` and `volatile` qualifiers and ignores references before evaluation. `typeid(T&)` and `typeid(const T)` both yield the `const std::type_info` for `T`.

**Mechanics Example:**

```cpp theme={"dark"}
#include <typeinfo>

struct Base { virtual ~Base() = default; };
struct Derived : Base {};
struct NonPolyBase {};
struct NonPolyDerived : NonPolyBase {};

int x = 5;
const int& rx = x;

// Compile-time evaluation
const std::type_info& t1 = typeid(int);
const std::type_info& t2 = typeid(rx);        // Yields type_info for 'int' (cv-ref stripped)
const std::type_info& t3 = typeid(x + 1.5);   // Yields type_info for 'double'

// Compile-time vs Run-time evaluation
Derived d;
Base& b_ref = d;
NonPolyDerived npd;
NonPolyBase& npb_ref = npd;

const std::type_info& t4 = typeid(b_ref);     // Run-time: Yields type_info for 'Derived' (polymorphic)
const std::type_info& t5 = typeid(npb_ref);   // Compile-time: Yields type_info for 'NonPolyBase' (static type)

// Null pointer dereference
Base* null_ptr = nullptr;
// const std::type_info& t6 = typeid(*null_ptr); // Throws std::bad_typeid at run-time
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
