> ## 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++ Local Class

A local class in C++ is a class defined within the lexical scope of a function or a block. While its name is only visible within that enclosing block, the type itself can escape the scope in modern C++ (C++14 and later) via `auto` return types, allowing objects of the local class to be instantiated outside the function using `decltype`.

```cpp theme={"dark"}
int global_var = 10;

auto enclosingFunction() {
    static int static_local = 20;
    int non_static_local = 30;
    constexpr int const_local = 40;

    // Local Class Declaration
    class LocalClass {
        int member_var;
    public:
        // Prohibited: Default arguments cannot use local variables of the enclosing function
        // LocalClass(int val = non_static_local) : member_var(val) {}

        LocalClass(int val) : member_var(val) {}

        // Member functions must be defined inline
        void process() {
            // Permitted: Accessing global variables
            global_var++;
            
            // Permitted: Accessing static variables of the enclosing function
            static_local++;
            
            // Permitted: Accessing const/constexpr non-odr-used local variables
            int array[const_local]; 
            
            // Prohibited: odr-use of non-static local variables
            // non_static_local++; 
        }

        // Permitted: Static member functions
        static void staticMethod() {}

        // Prohibited: Static data members
        // static int static_data; 
    };

    // Escaping the local scope via auto return type (C++14)
    return LocalClass(5);
}

void outsideFunction() {
    // Instantiating new objects of the local class outside its original lexical scope
    auto obj = enclosingFunction();
    decltype(obj) another_obj(10); 
}
```

## Core Technical Constraints

**1. Scope and Instantiation**
The class name is strictly resolved within the block where it is defined and cannot be forward-declared outside the function. However, since C++14, a function can return a local class type using an `auto` return type. This allows the type to escape its lexical scope, and new instances can be created outside the enclosing block using `decltype`.

**2. Member Function Definitions**
All member functions must be defined entirely within the class body (implicitly inline). Out-of-line definitions using the scope resolution operator (`::`) outside the class or function block are syntactically invalid.

**3. Enclosing Scope Access Rules**
A local class has restricted access to the variables of its enclosing function:

* **Non-static local variables:** Generally prohibited from being read or modified. However, a local class *is* permitted to access non-static local variables from the enclosing scope if they are usable in constant expressions (e.g., `constexpr` or `const int` variables) and are not odr-used (e.g., used in `sizeof`, `decltype`, or where the lvalue-to-rvalue conversion is immediately applied).
* **Static local variables:** Permitted. The class can read and modify `static` variables declared within the enclosing function.
* **Global namespace:** Permitted. The class can access global variables, global functions, type names (`typedef` or `using`), and enumerators that are visible at the point of the class definition.

**4. Default Arguments**
Member functions of a local class can specify default arguments. However, C++ explicitly prohibits these default arguments from evaluating or binding to local variables of the enclosing function.

**5. Static Members**
Local classes are prohibited from declaring `static` data members. The compiler cannot allocate storage for a static data member of a local class because the class itself has no linkage. Conversely, local classes are permitted to declare `static` member functions.

**6. Templates and Linkage**

* A local class cannot be a class template.
* A local class cannot contain member function templates.
* Local classes have no linkage. Prior to C++11, this meant they could not be passed as type arguments to templates. C++11 lifted this restriction, allowing local classes to be used as template type arguments (e.g., `std::vector<LocalClass>`), provided the template instantiation occurs within a valid scope where the type is known.

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