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.

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.
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.
Master C++ with Deep Grasping Methodology!Learn More