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 namespace is a declarative region that establishes a named lexical scope for identifiers such as classes, variables, and functions. It encapsulates its members, requiring explicit qualification via the scope resolution operator (::), a using declaration, or a using directive to access them from outside the scope, unless the identifiers are resolved implicitly through Argument-Dependent Lookup (ADL).

The Global Namespace

All identifiers not explicitly enclosed within a namespace block reside in the global namespace. This is the default declarative region for any translation unit. Members of the global namespace can be explicitly accessed using the unary scope resolution operator (::), which forces the compiler to resolve the identifier in the global scope, bypassing any local shadowing.
int value = 10; // Global namespace

namespace Core {
    int value = 20;
    
    void print() {
        int local_val = ::value; // Accesses the global 'value' (10)
    }
}

Standard Declaration

A namespace is declared using the namespace keyword followed by an identifier and a block containing the declarations.
namespace Core {
    int initialize();
    class Engine {
        // Class definition
    };
}
Namespaces are open-ended. Declaring a namespace with an identifier that already exists in the same scope does not create a new namespace or cause a redefinition error; instead, it extends the existing namespace.
namespace Core {
    // Extends the previously declared 'Core' namespace
    void shutdown(); 
}

Nested Namespace Declaration

Namespaces can be declared within other namespaces to create a hierarchical scope. Pre-C++17 Syntax:
namespace Network {
    namespace Protocol {
        class TCP;
    }
}
C++17 Syntax: C++17 introduced a concise syntax for nested namespace definitions, allowing multiple levels to be declared in a single line.
namespace Network::Protocol {
    class UDP;
}

Inline Namespace Declaration (C++11)

The inline specifier can be applied to a namespace declaration. Members of an inline namespace are automatically elevated to the enclosing namespace. Unlike a standard using directive, inline namespaces allow their members to participate in template specialization and Argument-Dependent Lookup (ADL) exactly as if they were direct members of the parent namespace.
namespace Library {
    inline namespace V2 {
        class Processor {};
        void execute(Processor p);
    }
}

// 'execute' and 'Processor' act as direct members of 'Library' for ADL and template specialization

Unnamed (Anonymous) Namespace Declaration

A namespace declared without an identifier is an unnamed namespace. The compiler implicitly generates a unique identifier for it. All identifiers declared within an unnamed namespace are given internal linkage, restricting their visibility strictly to the translation unit in which they are defined.
namespace {
    int internal_counter = 0;
    void helper_function() {
        // Implementation
    }
}

Namespace Alias Declaration

A namespace alias creates an alternative, typically shorter, identifier for an existing namespace. This is declared using the namespace keyword, the new alias name, the assignment operator, and the target namespace.
namespace NetProto = Network::Protocol;

// NetProto::UDP is now equivalent to Network::Protocol::UDP

Argument-Dependent Lookup (ADL)

Argument-Dependent Lookup (also known as Koenig lookup) is a compiler mechanism that resolves unqualified function calls by automatically searching the namespaces associated with the types of the function’s arguments. If a function is called without a scope resolution operator, the compiler inspects the namespaces of the provided arguments to find the matching function declaration.
namespace Data {
    struct Record {};
    void process(Record r); // Function in the same namespace as the argument type
}

void evaluate() {
    Data::Record myRecord;
    process(myRecord); // ADL finds Data::process automatically without 'Data::' qualification
}
Master C++ with Deep Grasping Methodology!Learn More