ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
using declaration introduces a specific name from a namespace, base class, or enumeration into the current declarative region. It creates a local synonym for an existing entity, allowing the compiler to resolve the unqualified identifier without requiring the fully qualified nested-name-specifier. Unlike a using directive (using namespace N;), which makes all names in a namespace available, a using declaration explicitly imports only a single specified name (or an overload set).
Syntax
nested-name-specifier: A sequence of names and scope resolution operators (e.g.,std::orBase<T>::) that specify the namespace or class where the name is originally declared. Note that the trailing::is inherently part of thenested-name-specifiergrammar.unqualified-id: The specific identifier, operator function, or conversion function being introduced.typename: Required when thenested-name-specifieris dependent on a template parameter and theunqualified-idrefers to a type....: C++17 syntax for expanding a parameter pack.
Scope and Resolution Mechanics
When ausing declaration is evaluated, the introduced name becomes a member of the declarative region in which the using declaration appears.
- Shadowing: The introduced name hides identical names declared in enclosing outer scopes.
- Collisions: It is ill-formed to introduce a name via a
usingdeclaration if a declaration for a different entity with the same name already exists in the exact same local scope, unless both are functions or function templates that form a valid overload set. Conversely, introducing the exact same entity multiple times viausingdeclarations in the same scope is perfectly valid.
Overload Sets
If theunqualified-id refers to a function, the using declaration introduces all overloaded functions with that name from the target scope. If the target scope adds new overloads after the using declaration, those new overloads are not added to the local scope.
Class Scope and Inheritance
Within a class definition, ausing declaration can introduce a member of a base class into the derived class’s scope. This alters the standard name lookup rules and access control.
- Unhiding: It prevents derived class members from hiding base class members with the same name but different signatures.
- Access Specification: The access level (public, protected, private) of the introduced name is determined by the access specifier applied to the
usingdeclaration in the derived class, overriding the inherited access level.
Dependent Types
When introducing a type from a dependent base class inside a class template, thetypename keyword is mandatory. This disambiguates the introduced name, instructing the compiler to treat it as a type rather than a non-type member.
Variadic Using Declarations (C++17)
C++17 permitsusing declarations to expand parameter packs. This is primarily utilized in class templates inheriting from a variadic pack of base classes, allowing the introduction of members from all base classes simultaneously.
Enumerations (C++20)
C++20 extends the syntax to support scoped enumerations viausing enum. This introduces all enumerators of the specified enumeration into the current declarative region.
Master C++ with Deep Grasping Methodology!Learn More





