> ## 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++ Alignof

The `alignof` operator is a compile-time unary operator that returns the alignment requirement of a specified type in bytes. It evaluates to a constant expression of type `std::size_t`, representing the number of bytes between successive memory addresses at which objects of the queried type can be allocated.

```cpp theme={"dark"}
alignof(type-id)
```

## Evaluation Rules and Mechanics

The behavior of `alignof` depends strictly on the category of the `type-id` provided as its operand:

* **Fundamental Types:** Returns the architecture-specific alignment boundary required by the hardware (e.g., typically `4` for `int` and `8` for `double` on 64-bit systems).
* **Class/Struct Types:** Returns the strictest (largest) alignment requirement among all its base classes and non-static data members. The alignment requirements of these subobjects dictate where the compiler must insert padding, but padding itself consists of empty space and does not factor into the `alignof` evaluation. The alignment value can also be explicitly overridden using the `alignas` specifier.
* **Array Types:** Returns the alignment requirement of the underlying element type, not the alignment or size of the entire array block.
* **Reference Types:** Returns the alignment requirement of the referenced type. The reference itself does not have a distinct alignment queried by this operator.

## Type Constraints

The `type-id` passed to `alignof` must be a **complete type**, an array type, or a reference type. Applying `alignof` to an incomplete type, a function type, or the `void` type is a constraint violation. This renders the program ill-formed and results in a standard compilation error, as the compiler cannot determine the alignment for these types.

## Syntax Visualization

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

struct Base {
    double d;    // alignof(double) == 8
};

struct Derived : Base {
    char c;      // alignof(char) == 1
    int i;       // alignof(int) == 4
};

struct alignas(32) OveralignedStruct {
    int i;
};

int main() {
    // Evaluates to the architecture's default alignment for int
    constexpr std::size_t int_align = alignof(int); 

    // Evaluates to 8, driven by the strictest requirement from the Base class
    constexpr std::size_t struct_align = alignof(Derived); 

    // Evaluates to 32, driven by the alignas specifier
    constexpr std::size_t custom_align = alignof(OveralignedStruct); 

    // Evaluates to alignof(int), ignoring the array bounds
    constexpr std::size_t array_align = alignof(int[100]); 

    // Evaluates to alignof(double), ignoring the reference semantics
    constexpr std::size_t ref_align = alignof(double&); 

    return 0;
}
```

Because `alignof` resolves entirely during compilation, its result is guaranteed to be a core constant expression.

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