Prec.      Page 21     Suiv.

 

3 Basic concepts

[basic

  1. [Note: this clause presents the basic concepts of the C++ language. It explains the difference between an object and a name and how they relate to the notion of an lvalue. It introduces the concepts of a declaration and a definition and presents C++'s notion of type, scope, linkage, and storage duration. The mechanisms for starting and terminating a program are discussed. Finally, this clause presents the fundamental types of the language and lists the ways of constructing compound types from these.
  2. This clause does not cover concepts that affect only a single part of the language. Such concepts are discussed in the relevant clauses.]
  3. An entity is a value, object, subobject, base class subobject, array element, variable, function, instance of a function, enumerator, type, class member, template, or namespace.
  4. A name is a use of an identifier (2.10) that denotes an entity or label (6.6.4, 6.1). A variable is introduced by the declaration of an object. The variable's name denotes the object.
  5. Every name that denotes an entity is introduced by a declaration. Every name that denotes a label is introduced either by a goto statement (6.6.4) or a labeled statement (6.1).
  6. Some names denote types, classes, enumerations, or templates. In general, it is necessary to determine whether or not a name denotes one of these entities before parsing the program that contains it. The process that determines this is called name lookup (3.4).
  7. Two names are the same if
  8. An identifier used in more than one translation unit can potentially refer to the same entity in these translation units depending on the linkage (3.5) of the identifier specified in each translation unit.

3.1 Declarations and definitions

[basic.def

  1. A declaration (clause 7) introduces names into a translation unit or redeclares names introduced by previous declarations. A declaration specifies the interpretation and attributes of these names.
  2. A declaration is a definition unless it declares a function without specifying the function's body (8.4), it contains the extern specifier (7.1.1) or a linkage­specification 24) (7.5) and neither an initializer nor a function­body, it declares a static data member in a class declaration (9.4), it is a class name declaration (9.1), or it is a typedef declaration (7.1.3), a using­declaration (7.3.3), or a using­directive (7.3.4).

  3.  

    Prec.      Page 22     Suiv.
  4. [Example: all but one of the following are definitions:
  5.     int a;                          // defines a
        extern const int c = 1;         //
    defines c
        int f (int x) { return x + a; } //
    defines f and defines x
        struct S { int a; int b; };     //
    defines S, S::a, and S::b
        struct X {                      //
    defines X
            int x;                      //
    defines nonstatic data member x
            static int y;               //
    declares static data member y
            X() : x (0) { }             //
    defines a constructor of X
        };
        int X::y = 1;                   //
    defines X::y
        enum { up, down };              //
    defines up and down
        namespace N { int d; }          //
    defines N and N::d
        namespace N1 = N;               //
    defines N1
        X anX;                          //
    defines anX

    whereas these are just declarations:

        extern int a;       // declares a
        extern const int c; //
    declares c
        int f (int);        //
    declares f
        struct S;           //
    declares S
        typedef int Int;    //
    declares Int
        extern X anotherX;  //
    declares anotherX
        using N::d;          //
    declares N::d

    --end example]

  6. [Note: in some circumstances, C++ implementations implicitly define the default constructor (12.1), copy constructor (12.8), assignment operator (12.8), or destructor (12.4) member functions. [Example: given
  7.              

        struct C {
            string s; // string
    is the standard library class (clause 21)
        };

        int main()
        {
             C a;
             C b = a;
             b = a;
        }

    the implementation will implicitly define functions to make the definition of C equivalent to

        struct C {
            string s;
            C () : s () { }
            C (const C& x) : s (x. s) { }
            C & operator = (const C& x) { s = x.s; return *this; }
            ~C () { }
        };

    --end example] --end note]

  8. [Note: a class name can also be implicitly declared by an elaborated­type­specifier (3.3.1).]
  9. A program is ill­formed if the definition of any object gives the object an incomplete type (3.9).

3.2 One definition rule

[basic.def.odr

  1. No translation unit shall contain more than one definition of any variable, function, class type, enumeration type or template.

  2.  

    Prec.       Page 23     Suiv.

  3. An expression is potentially evaluated unless either it is the operand of the sizeof operator (5.3.3), or it is the operand of the typeid operator and does not designate an lvalue of polymorphic class type (5.2.8). An object or non­overloaded function is used if its name appears in a potentially­evaluated expression. A virtual member function is used if it is not pure. An overloaded function is used if it is selected by overload resolution when referred to from a potentially­evaluated expression. [Note: this covers calls to named functions (5.2.2), operator overloading (clause 13), user­defined conversions (12.3.2), allocation function for placement new (5.3.4), as well as non­default initialization (8.5). A copy constructor is used even if the call is actually elided by the implementation.] An allocation or deallocation function for a class is used by a new expression appearing in a potentially­evaluated expression as specified in 5.3.4 and 12.5. A deallocation function for a class is used by a delete expression appearing in a potentially­evaluated expression as specified in 5.3.5 and 12.5. A copy­assignment function for a class is used by an implicitly­defined copy­assignment function for another class as specified in 12.8. A default constructor for a class is used by default initialization as specified in 8.5. A constructor for a class is used as specified in 8.5. A destructor for a class is used as specified in 12.4.
  4. Every program shall contain exactly one definition of every non­inline function or object that is used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user­defined library, or (when appropriate) it is implicitly defined (see 12.1, 12.4 and 12.8). An inline function shall be defined in every translation unit in which it is used.
  5. Exactly one definition of a class is required in a translation unit if the class is used in a way that requires the class type to be complete. [Example: the following complete translation unit is well­formed, even though it never defines X:
  6. struct X;     // declare X as a struct type
    struct X* x1; // use X in pointer formation
    X* x2;        // use X in pointer formation
    --end example] [Note: the rules for declarations and expressions describe in which contexts complete class types are required. A class type T must be complete if:

  7. There can be more than one definition of a class type (clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (clause 14), non­static function template (14.5.5), static data member of a class template (14.5.1.3), member function template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.4) in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then

3.2 Declarative regions and scopes

[basic.scope

  1. Every name is introduced in some portion of program text called a declarative region, which is the largest part of the program in which that name is valid, that is, in which that name may be used as an unqualified name to refer to the same entity. In general, each particular name is valid only within some possibly discontiguous portion of program text called its scope. To determine the scope of a declaration, it is sometimes convenient to refer to the potential scope of a declaration. The scope of a declaration is the same as its potential scope unless the potential scope contains another declaration of the same name. In that case, the potential scope of the declaration in the inner (contained) declarative region is excluded from the scope of the declaration in the outer (containing) declarative region.

  2.  

    Prec.      Page 25     Suiv.
  3. [Example: in
  4.     int j = 24;
        int main()
        {
            int i = j, j;
            j = 42;
        }

    the identifier j is declared twice as a name (and used twice). The declarative region of the first j includes the entire example. The potential scope of the first j begins immediately after that j and extends to the end of the program, but its (actual) scope excludes the text between the , and the }. The declarative region of the second declaration of j (the j immediately before the semicolon) includes all the text between { and }, but its potential scope excludes the declaration of i. The scope of the second declaration of j is the same as its potential scope.]

  5. The names declared by a declaration are introduced into the scope in which the declaration occurs, except that the presence of a friend specifier (11.4), certain uses of the elaborated­type­specifier (3.3.1), and using­directives (7.3.4) alter this general behavior.

  6.  
  7. Given a set of declarations in a single declarative region, each of which specifies the same unqualified name,

    [Note: these restrictions apply to the declarative region into which a name is introduced, which is not necessarily the same as the region in which the declaration occurs. In particular, elaborated­type­specifiers (3.3.1) and friend declarations (11.4) may introduce a (possibly not visible) name into an enclosing name­space; these restrictions apply to that region. Local extern declarations (3.5) may introduce a name into the declarative region where the declaration appears and also introduce a (possibly not visible) name into an enclosing namespace; these restrictions apply to both regions.]

  8. [Note: the name lookup rules are summarized in 3.4.]

3.3.1 Point of declaration

[basic.scope.pdecl

  1. The point of declaration for a name is immediately after its complete declarator (clause 8) and before its initializer (if any), except as noted below. [Example:
  2. int x = 12;
    { int x = x; }
    Here the second x is initialized with its own (indeterminate) value.]

  3. [Note: a nonlocal name remains visible up to the point of declaration of the local name that hides it.
    [Example:
    const int i = 2;
    { int i [i]; }
    declares a local array of two integers.]]

  4.  
  5. The point of declaration for an enumerator is immediately after its enumerator­definition. [Example:
  6. const int x = 12;
    { enum { x = x }; }
    Here, the enumerator x is initialized with the value of the constant x, namely 12.]

    Prec.      Page 26     Suiv.

  7. After the point of declaration of a class member, the member name can be looked up in the scope of its class. [Note: this is true even if the class is an incomplete class. For example,

        struct X {
            enum E { z = 16 };
            int b [X::z]; //
    OK
        };

    --end note]

  8. The point of declaration of a class first declared in an elaborated­type­specifier is as follows:
     

  9. [Note: friend declarations refer to functions or classes that are members of the nearest enclosing namespace, but they do not introduce new names into that namespace (7.3.1.2). Function declarations at block scope and object declarations with the extern specifier at block scope refer to delarations that are members of an enclosing namespace, but they do not introduce new names into that scope.]
  10. [Note: For point of instantiation of a template, see 14.7.1.]

3.3.2 Local scope

[basic.scope.local

  1. A name declared in a block (6.3) is local to that block. Its potential scope begins at its point of declaration (3.3.1) and ends at the end of its declarative region.
  2. The potential scope of a function parameter name in a function definition (8.4) begins at its point of declaration. If the function has a function try­block the potential scope of a parameter ends at the end of the last associated handler, else it ends at the end of the outermost block of the function definition. A parameter name shall not be redeclared in the outermost block of the function definition nor in the outermost block of any handler associated with a function try­block.
  3. The name in a catch exception­declaration is local to the handler and shall not be redeclared in the outer­most block of the handler.
  4. Names declared in the for­init­statement, and in the condition of if, while, for, and switch statements are local to the if, while, for, or switch statement (including the controlled statement), and shall not be redeclared in a subsequent condition of that statement nor in the outermost block (or, for the if statement, any of the outermost blocks) of the controlled statement; see 6.4.

3.3.3 Function prototype scope

[basic.scope.proto]

  1. In a function declaration, or in any function declarator except the declarator of a function definition (8.4), names of parameters (if supplied) have function prototype scope, which terminates at the end of the nearest enclosing function declarator.

  2.  

    Prec.      Page 27     Suiv.

3.3.4 Function scope

[basic.funscope]

  1. Labels (6.1) have function scope and may be used anywhere in the function in which they are declared. Only labels have function scope.

33.3.5 Namespace scope

[basic.scope.namespace]

  1. The declarative region of a namespace­definition is its namespace­body. The potential scope denoted by an original­namespace­name is the concatenation of the declarative regions established by each of the namespace­definitions in the same declarative region with that original­namespace­name. Entities declared in a namespace­body are said to be members of the namespace, and names introduced by these declarations into the declarative region of the namespace are said to be member names of the namespace. A namespace member name has namespace scope. Its potential scope includes its namespace from the name's point of declaration (3.3.1) onwards; and for each using­directive (7.3.4) that nominates the member's namespace, the member's potential scope includes that portion of the potential scope of the using­directive that follows the member's point of declaration. [Example:

        namespace N {
            int i;
            int g (int a) { return a; }
            int j ();
            void q ();

        }
        namespace { int l = 1; }
        //
    the potential scope of l is from its point of declaration
        //
    to the end of the translation unit

        namespace N {
            int g (char a)     //
    overloads N::g (int)
            {
                return l + a;  // l
    is from unnamed namespace
            }

            int i;             // error: duplicate definition
            int j ();          //
    OK: duplicate function declaration

            int j ();          // OK: definition of N::j ()
            {
                return g (i);  //
    calls N::g (int)
            }
            int q ();          //
    error: different return type
            }

    --end example]

  2. A namespace member can also be referred to after the :: scope resolution operator (5.1) applied to the name of its namespace or the name of a namespace which nominates the member's namespace in a using­directive; see 3.4.3.2.
  3. A name declared outside all named or unnamed namespaces (7.3), blocks (6.3), function declarations (8.3.5), function definitions (8.4) and classes (clause 9) has global namespace scope (also called global scope). The potential scope of such a name begins at its point of declaration (3.3.1) and ends at the end of the translation unit that is its declarative region. Names declared in the global namespace scope are said to be global.

3.3.6 Class scope

[basic.scope.class]

  1. The following rules describe the scope of names declared in classes.

    1. The potential scope of a name declared in a class consists not only of the declarative region following the name's declarator, but also of all function bodies, default arguments, and constructor ctor-initializers in that class (including such things in nested classes).

    2.  

      Prec.      Page 28     Suiv.

    3. A name N used in a class S shall refer to the same declaration in its context and when re­evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.
    4. If reordering member declarations in a class yields an alternate valid program under (1) and (2), the program is ill­formed, no diagnostic is required.
    5. A name declared within a member function hides a declaration of the same name whose scope extends to or past the end of the member function's class.
    6. The potential scope of a declaration that extends to or past the end of a class definition also extends to the regions defined by its member definitions, even if the members are defined lexically outside the class (this includes static data member definitions, nested class definitions, member function definitions (including the member function body and, for constructor functions (12.1), the ctor­initializer (12.6.2)) and any portion of the declarator part of such definitions which follows the identifier, including a parameter­declaration­clause and any default arguments (8.3.6). [Example:

          typedef int c;
          enum { i = 1 };
          class X {
              char v [i];                     //
      error: i refers to ::i
                                              //
      but when reevaluated is X::i
              int f () { return sizeof (c); } //
      OK: X::c
              char c;
              enum { i = 2 };
          };

          typedef char* T;
          struct Y {
              T a;                            //
      error: T refers to ::T
                                              //
      but when reevaluated is Y::T
              typedef long T;
              T b;
          };

          typedef int I;
          class D {
              typedef I I;                    //
      error, even though no reordering involved
          };

      --end example]

  2. The name of a class member shall only be used as follows:
     

3.3.7 Name hiding

[basic.scope.hiding]

  1. A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class (10.2).
  2. A class name (9.1) or enumeration name (7.2) can be hidden by the name of an object, function, or enumerator declared in the same scope. If a class or enumeration name and an object, function, or enumerator are declared in the same scope (in any order) with the same name, the class or enumeration name is hidden wherever the object, function, or enumerator name is visible.

  3. Prec.      Page 29     Suiv.

  4. In a member function definition, the declaration of a local name hides the declaration of a member of the class with the same name; see 3.3.6. The declaration of a member in a derived class (clause 10) hides the declaration of a member of a base class of the same name; see 10.2.
  5. During the lookup of a name qualified by a namespace name, declarations that would otherwise be made visible by a using­directive can be hidden by declarations with the same name in the namespace containing the using­directive; see (3.4.3.2).
  6. If a name is in scope and is not hidden it is said to be visible.

3.4 Name lookup

[basic.lookup]

  1. The name lookup rules apply uniformly to all names (including typedef­names (7.1.3), namespace­names (7.3) and class­names (9.1)) wherever the grammar allows such names in the context discussed by a particular rule. Name lookup associates the use of a name with a declaration (3.1) of that name. Name lookup shall find an unambiguous declaration for the name (see 10.2). Name lookup may associate more than one declaration with a name if it finds the name to be a function name; the declarations are said to form a set of overloaded functions (13.1). Overload resolution (13.3) takes place after name lookup has succeeded. The access rules (clause 11) are considered only once name lookup and function overload resolution (if applicable) have succeeded. Only after name lookup, function overload resolution (if applicable) and access checking have succeeded are the attributes introduced by the name's declaration used further in expression processing (clause 5).
  2. A name "looked up in the context of an expression" is looked up as an unqualified name in the scope where the expression is found.
  3. Because the name of a class is inserted in its class scope (clause 9), the name of a class is also considered a member of that class for the purposes of name hiding and lookup.
  4. [Note: 3.5 discusses linkage issues. The notions of scope, point of declaration and name hiding are discussed in 3.3.]

3.4.1 Unqualified name lookup

[basic.lookup.unqual]

  1. In all the cases listed in 3.4.1, the scopes are searched for a declaration in the order listed in each of the respective categories; name lookup ends as soon as a declaration is found for the name. If no declaration is found, the program is ill­formed.
  2. The declarations from the namespace nominated by a using­directive become visible in a namespace enclosing the using­directive; see 7.3.4. For the purpose of the unqualified name lookup rules described in3.4.1, the declarations from the namespace nominated by the using­directive are considered members of that enclosing namespace.
  3. The lookup for an unqualified name used as the postfix­expression of a function call is described in 3.4.2. [Note: for purposes of determining (during parsing) whether an expression is a postfix­expression for a function call, the usual name lookup rules apply. The rules in 3.4.2 have no effect on the syntactic interpretation of an expression. For example,

        typedef int f;
        struct A {
            friend void f (A &);
            operator int ();
            void g (A a) {
                f (a);
            }
        };

    The expression f (a) is a cast­expression equivalent to int (a). Because the expression is not a function call, the argument­dependent name lookup (3.4.2) does not apply and the friend function f is not found.]


  4.  

    Prec.     Page 30     Suiv.

  5. A name used in global scope, outside of any function, class or user­declared namespace, shall be declared before its use in global scope.
  6. A name used in a user­declared namespace outside of the definition of any function or class shall be declared before its use in that namespace or before its use in a namespace enclosing its namespace.
  7. A name used in the definition of a function 26) that is a member of namespace N (where, only for the purpose of exposition, N could represent the global scope) shall be declared before its use in the block in which it is used or in one of its enclosing blocks (6.3) or, shall be declared before its use in namespace N or, if N is a nested namespace, shall be declared before its use in one of N's enclosing namespaces. [Example:

        namespace A {
            namespace N {
                void f();
            }
        }

        void A::N::f() {
            i = 5;
                   //
    The following scopes are searched for a declaration of i:
                   //
    1) outermost block scope of A::N::f, before the use of i
                   //
    2) scope of namespace N
                   //
    3) scope of namespace A
                   //
    4) global scope, before the definition of A::N::f
        }

    --end example]

  8. A name used in the definition of a class X outside of a member function body or nested class definition 27) shall be declared in one of the following ways:
     

    Prec.      Page 31     Suiv.

    [Example:

        namespace M {
            class B { };
        }

        namespace N {
            class Y : public M::B {
                class X {
                    int a [i];
                };
            };
        };

        // The following scopes are searched for a declaration of i:
        //
    1) scope of class N::Y::X, before the use of i
        //
    2) scope of class N::Y, before the definition of N::Y::X
        //
    3) scope of N::Y's base class M::B
        //
    4) scope of namespace N, before the definition of N::Y
        //
    5) global scope, before the definition of N

    --end example] [Note: when looking for a prior declaration of a class or function introduced by a friend declaration, scopes outside of the innermost enclosing namespace scope are not considered; see 7.3.1.2.] [Note: 3.3.6 further describes the restrictions on the use of names in a class definition. 9.7 further describes the restrictions on the use of names in nested class definitions. 9.8 further describes the restrictions on the use of names in local class definitions.]

  9. A name used in the definition of a function that is a member function (9.3) 29) of class X shall be declared in one of the following ways:
     
     

    Prec.      Page 32     Suiv.

    [Example:

        class B { };
        namespace M {
            namespace N {
                class X : public B {
                    void f();
                };
            }
        }
        void M::N::X::f() {
            i = 16;
        }

        // The following scopes are searched for a declaration of i:
        //
    1) outermost block scope of M::N::X::f, before the use of i
        //
    2) scope of class M::N::X
        //
    3) scope of M::N::X's base class B
        //
    4) scope of namespace M::N
        //
    5) scope of namespace M
        //
    6) global scope, before the definition of M::N::X::f

  10. --end example] [Note: 9.3 and 9.4 further describe the restrictions on the use of names in member function definitions. 9.7 further describes the restrictions on the use of names in the scope of nested classes. 9.8 further describes the restrictions on the use of names in local class definitions.]

  11. Name lookup for a name used in the definition of a friend function (11.4) defined inline in the class granting friendship shall proceed as described for lookup in member function definitions. If the friend function is not defined in the class granting friendship, name lookup in the friend function definition shall proceed as described for lookup in namespace member function definitions.
  12. In a friend declaration naming a member function, a name used in the function declarator and not part of a template­argument in a template­id is first looked up in the scope of the member function's class. If it is not found, or if the name is part of a template­argument in a template­id, the look up is as described for unqualified names in the definition of the class granting friendship. [Example:

        struct A {
            typedef int AT;
            void f1 (AT);
            void f2 (float);
        };
        struct B {
            typedef float BT;
            friend void A::f1 (AT); //
    parameter type is A::AT
            friend void A::f2 (BT); //
    parameter type is B::BT
        };

    --end example]

  13. During the lookup for a name used as a default argument (8.3.6) in a function parameter­declaration­clause or used in the expression of a mem­initializer for a constructor (12.6.2), the function parameter names are visible and hide the names of entities declared in the block, class or namespace scopes containing the function declaration. [Note: 8.3.6 further describes the restrictions on the use of names in default arguments. 12.6.2 further describes the restrictions on the use of names in a ctor­initializer.]
  14. A name used in the definition of a static data member of class X (9.4.2) (after the qualified­id of the static member) is looked up as if the name was used in a member function of X. [Note: 9.4.2 further describes the restrictions on the use of names in the definition of a static data member.]

  15.  
  16. A name used in the handler for a function­try­block (clause 15) is looked up as if the name was used in the
    outermost block of the function definition. In particular, the function parameter names shall not be redeclared in the exception­declaration nor in the outermost block of a handler for the function­try­block. Names declared in the outermost block of the function definition are not found when looked up in the scope of a handler for the function­try­block. [Note: but function parameter names are found.]

  17.  
  18. [Note: the rules for name lookup in template definitions are described in 14.6.]

3.4.2 Argument­dependent name lookup

[basic.lookup.koenig]

  1. When an unqualified name is used as the postfix­expression in a function call (5.2.2), other namespaces not considered during the usual unqualified lookup (3.4.1) may be searched, and namespace­scope friend func­tion declarations (11.4) not otherwise visible may be found. These modifications to the search depend on the types of the arguments (and for template template arguments, the namespace of the template argument).

  2. Prec.      Page 33     Suiv.

     
  3. For each argument type T in the function call, there is a set of zero or more associated namespaces and a set of zero or more associated classes to be considered. The sets of namespaces and classes is determined entirely by the types of the function arguments (and the namespace of any template template argument). Typedef names and using­declarations used to specify the types do not contribute to this set. The sets of namespaces and classes are determined in the following way:
     
    If the ordinary unqualified lookup of the name finds the declaration of a class member function, the associated namespaces and classes are not considered. Otherwise the set of declarations found by the lookup of the function name is the union of the set of declarations found using ordinary unqualified lookup and the set of declarations found in the namespaces and classes associated with the argument types. [Example:

        namespace NS {
            class T { };
            void f (T);
        }
        NS::T parm;
        int main () {
            f (parm); //
    OK: calls NS::f
        }

    --end example]

  4. When considering an associated namespace, the lookup is the same as the lookup performed when the associated namespace is used as a qualifier (3.4.3.2) except that:

3.4.3 Qualified name lookup

[basic.lookup.qual]

  1. The name of a class or namespace member can be referred to after the :: scope resolution operator (5.1) applied to a nested­name­specifier that nominates its class or namespace. During the lookup for a name preceding the :: scope resolution operator, object, function, and enumerator names are ignored. If the name found is not a class­name (clause 9) or namespace­name (7.3.1), the program is ill­formed. [Example:

    Prec.      Page 34     Suiv.

        class A {
          public :
            static int n;
        };
        int main()
        {
            int A;
            A::n = 42; //
    OK
            A b;       //
    ill­formed: A does not name a type
        }

    --end example]

  2. [Note: Multiply qualified names, such as N1::N2::N3::n, can be used to refer to members of nested classes (9.7) or members of nested namespaces.]

  3. In a declaration in which the declarator­id is a qualified­id, names used before the qualified­id being declared are looked up in the defining namespace scope; names following the qualified­id are looked up in the scope of the member's class or namespace. [Example:

        class X { };
        class C {
            class X { };
            static const int number = 50;
            static X arr [number];
        };
        X C::arr [number]; //
    ill­formed:
                           //
    equivalent to: ::X C::arr [C::number];
                           //
    not to: C::X C::arr [C::number];

    --end example]

  4. A name prefixed by the unary scope operator :: (5.1) is looked up in global scope, in the translation unit where it is used. The name shall be declared in global namespace scope or shall be a name whose declaration is visible in global scope because of a using­directive (3.4.3.2). The use of :: allows a global name to be referred to even if its identifier has been hidden (3.3.7).

  5. If a pseudo­destructor­name (5.2.4) contains a nested­name­specifier, the type­names are looked up as types in the scope designated by the nested­name­specifier. In a qualified­id of the form:
    ::opt nested­name­specifier ~ class­name
    where the nested­name­specifier designates a namespace scope, and in a qualified­id of the form:
    ::opt nested­name­specifier class­name::~ class­name
    the class­names are looked up as types in the scope designated by the nested­name­specifier. [Example:

        struct C {
            typedef int I;
        };
        typedef int I1, I2;
        extern int* p;
        extern int* q;
        p­>C::I::~I();                    // I
    is looked up in the scope of C
        q­>I1::~I2();                     // I2
    is looked up in the scope of
                                          //
    the postfix­expression
        struct A {
            ~A();
        };
        typedef A AB;
        int main()
        {
            AB *p;
            p­>AB::~AB();                 //
    explicitly calls the destructor for A
        }

    --end example] [Note: 3.4.5 describes how name lookup proceeds after the . and ­> operators.]


Prec.      Page 35     Suiv.

3.4.3.1 Class members

[class.qual]

  1. If the nested­name­specifier of a qualified­id nominates a class, the name specified after the nested­name­specifier is looked up in the scope of the class (10.2), except for the cases listed below. The name shall rep­resent one or more members of that class or of one of its base classes (clause 10). [Note: a class member can be referred to using a qualified­id at any point in its potential scope (3.3.6).] The exceptions to the name lookup rule above are the following:
  2. A class member name hidden by a name in a nested declarative region or by the name of a derived class member can still be found if qualified by the name of its class followed by the :: operator.

3.4.3.2 Namespace members

[namespace.qual]

  1. If the nested­name­specifier of a qualified­id nominates a namespace, the name specified after the nested­name­specifier is looked up in the scope of the namespace, except that the template­arguments of a template­id are looked up in the context in which the entire postfix­expression occurs.
  2. Given X::m (where X is a user­declared namespace), or given ::m (where X is the global namespace), let S be the set of all declarations of m in X and in the transitive closure of all namespaces nominated by using­directives in X and its used namespaces, except that using­directives are ignored in any namespace, including X, directly containing one or more declarations of m. No namespace is searched more than once in the lookup of a name. If S is the empty set, the program is ill­formed. Otherwise, if S has exactly one member, or if the context of the reference is a using­declaration (7.3.3), Sis the required set of declarations of m. Otherwise if the use of m is not one that allows a unique declaration to be chosen from S, the program is ill­formed. [Example:

    Prec.      Page 36     Suiv.

        int x;
        namespace Y {
            void f (float);
            void h (int);
        }
        namespace Z {
            void h (double);
        }

        namespace A {
            using namespace Y;
            void f (int);
            void g (int);
            int i;
        }

        namespace B {
            using namespace Z;
            void f (char);
            int i;
        }

        namespace AB {
            using namespace A;
            using namespace B;
            void g();
        }

        void h()
        {
            AB::g();      // g
    is declared directly in AB,
                          //
    therefore S is { AB::g() } and AB::g() is chosen
            AB::f(1);     // f
    is not declared directly in AB so the rules are
                          //
    applied recursively to A and B;
                          //
    namespace Y is not searched and Y::f (float)
                          //
    is not considered;
                          // S
    is { A::f (int), B::f (char) } and overload
                          //
    resolution chooses A::f (int)
            AB::f('c');   //
    as above but resolution chooses B::f (char)

            AB::x++;      // x is not declared directly in AB, and
                          //
    is not declared in A or B, so the rules are
                          //
    applied recursively to Y and Z,
                          // S
    is { } so the program is ill­formed
            AB::i++;      // i
    is not declared directly in AB so the rules are
                          //
    applied recursively to A and B,
                          // S
    is { A::i, B::i } so the use is ambiguous
                          //
    and the program is ill­formed
            AB::h (16.8); // h
    is not declared directly in AB and
                          //
    not declared directly in A or B so the rules are
                          //
    applied recursively to Y and Z,
                          // S
    is { Y::h (int), Z::h (double) and overload
                          //
    resolution chooses Z::h (double)
        }

  3. The same declaration found more than once is not an ambiguity (because it is still a unique declaration).
    For example:

        namespace A {
            int a;
        }

        namespace B {
            using namespace A;
        }

        namespace C {
            using namespace A;
        }


    Prec.      Page 37     Suiv.

        namespace BC {
            using namespace B;
            using namespace C;
        }

        void f()
        {
            BC::a++; //
    OK: S is { A::a, A::a }
        }

        namespace D {
            using A::a;
        }

        namespace BD {
            using namespace B;
            using namespace D;
        }

        void g()
        {
            BD::a++; //
    OK: S is { A::a, A::a }
        }

  4. Because each referenced namespace is searched at most once, the following is well­defined:

        namespace B {
            int b;
        }

        namespace A {
            using namespace B;
            int a;
        }

        namespace B {
            using namespace A;
        }

        void f()
        {
            A::a++; //
    OK: a declared directly in A, S is { A::a }
            B::a++; //
    OK: both A and B searched (once), S is { A::a }
            A::b++; //
    OK: both A and B searched (once), S is { B::b }
            B::b++; //
    OK: b declared directly in B, S is { B::b }
        }

    --end example]

  5. During the lookup of a qualified namespace member name, if the lookup finds more than one declaration of the member, and if one declaration introduces a class name or enumeration name and the other declarations either introduce the same object, the same enumerator or a set of functions, the non­type name hides the class or enumeration name if and only if the declarations are from the same namespace; otherwise (the declarations are from different namespaces), the program is ill­formed. [Example:

        namespace A {
            struct x { };
            int x;
            int y;
        }


    Prec.      Page 38     Suiv.

        namespace B {
            struct y {};
        }

        namespace C {
            using namespace A;
            using namespace B;
            int i = C::x; //
    OK, A::x (of type int)
            int j = C::y; //
    ambiguous, A::y or B::y
        }

    --end example]

  6. In a declaration for a namespace member in which the declarator­id is a qualified­id, given that the qualified­id for the namespace member has the form

    nested­name­specifier unqualified­id

    the unqualified­id shall name a member of the namespace designated by the nested­name­specifier. [Example:

        namespace A {
            namespace B {
                void f1 (int);
            }
            using namespace B;
        }
        void A::f1 (int) { } //
    ill­formed, f1 is not a member of A

    --end example] However, in such namespace member declarations, the nested­name­specifier may rely on using­directives to implicitly provide the initial part of the nested­name­specifier. [Example:

        namespace A {
            namespace B {
                void f1 (int);
            }
        }

        namespace C {
            namespace D {
                void f1 (int);
            }
        }

        using namespace A;
        using namespace C::D;
        void B::f1 (int){} //
    OK, defines A::B::f1 (int)

    --end example]

3.4.4 Elaborated type specifiers

[basic.lookup.elab]

  1. An elaborated­type­specifier may be used to refer to a previously declared class­name or enum­name even though the name has been hidden by a non­type declaration (3.3.7). The class­name or enum­name in the elaborated­type­specifier may either be a simple identifer or be a qualified­id.

  2. If the name in the elaborated­type­specifier is a simple identifer, and unless the elaborated­type­specifier has the following form:
    class­key identifier ;

    the identifier is looked up according to 3.4.1 but ignoring any non­type names that have been declared. If this name lookup finds a typedef­name, the elaborated­type­specifier is ill­formed. If the elaborated­type­specifier refers to an enum­name and this lookup does not find a previously declared enum­name, the


    Prec.      Page 39     Suiv.

    elaborated­type­specifier is ill­formed. If the elaborated­type­specifier refers to an class­name and this lookup does not find a previously declared class­name, or if the elaborated­type­specifier has the form:

    class­key identifier ;

    the elaborated­type­specifier is a declaration that introduces the class­name as described in 3.3.1.

  3. If the name is a qualified­id, the name is looked up according its qualifications, as described in 3.4.3, but ignoring any non­type names that have been declared. If this name lookup finds a typedef­name, the elaborated­type­specifier is ill­formed. If this name lookup does not find a previously declared class­name or enum­name, the elaborated­type­specifieris ill­formed. [Example:

        struct Node {
            struct Node* Next; //
    OK: Refers to Node atglobal scope
            struct Data* Data; //
    OK: Declares type Data
                               //
    at global scope and member Data
        };
        struct Data {
            struct Node* Node;     //
    OK: Refers to Node at global scope
            friend struct ::Glob;  //
    error: Glob is not declared
                                   //
    cannot introduce a qualified type (7.1.5.3)
            friend struct Glob;    //
    OK: Refers to (as yet) undeclared Glob
                                   //
    at global scope.
              /* ... */
        };

        struct Base {
            struct Data;                  //
    OK: Declares nested Data
            struct ::Data*     thatData;  //
    OK: Refers to ::Data
            struct Base::Data* thisData;  //
    OK: Refers to nested Data
            friend class ::Data;          //
    OK: global Data is a friend
            friend class Data;            //
    OK: nested Data is a friend
            struct Data { /* ... */ };    //
    Defines nested Data
            struct Data;                  //
    OK: Redeclares nested Data
        };

        struct Data;               // OK: Redeclares Data at global scope
        struct ::Data;             //
    error: cannot introduce a qualified type (7.1.5.3)
        struct Base::Data;         //
    error: cannot introduce a qualified type (7.1.5.3)
        struct Base::Datum;        //
    error: Datum undefined
        struct Base::Data* pBase;  //
    OK: refers to nested Data

    --end example]

3.4.5 Class member access

[basic.lookup.classref]

  1. In a class member access expression (5.2.5), if the . or ­> token is immediately followed by an identifier followed by a <, the identifier must be looked up to determine whether the < is the beginning of a template argument list (14.2) or a less­than operator. The identifier is first looked up in the class of the object expression. If the identifier is not found, it is then looked up in the context of the entire postfix­expression and shall name a class or function template. If the lookup in the class of the object expression finds a template, the name is also looked up in the context of the entire postfix­expression and

    -- if the name is not found, the name found in the class of the object expression is used, otherwise

    -- if the name is found in the context of the entire postfix­expression and does not name a class template, the name found in the class of the object expression is used, otherwise

    -- if the name found is a class template, it must refer to the same entity as the one found in the class of the object expression, otherwise the program is ill­formed.


  2. Prec.      Page 40     Suiv.

  3. If the id­expression in a class member access (5.2.5) is an unqualified­id, and the type of the object expression is of a class type C (or of pointer to a class type C), the unqualified­id is looked up in the scope of class C. If the type of the object expression is of pointer to scalar type, the unqualified­id is looked up in the context of the complete postfix­expression.
  4. If the unqualified­id is ~type­name, and the type of the object expression is of a class type C (or of pointer to a class type C), the type­name is looked up in the context of the entire postfix­expression and in the scope of class C. The type­name shall refer to a class­name. If type­name is found in both contexts, the name shall refer to the same class type. If the type of the object expression is of scalar type, the type­name is looked up in the scope of the complete postfix­expression.
  5. If the id­expression in a class member access is a qualified­ id of the form class­name­or­namespace­name::...

    the class­name­or­namespace­name following the . or ­> operator is looked up both in the context of the entire postfix­expression and in the scope of the class of the object expression. If the name is found only in the scope of the class of the object expression, the name shall refer to a class­name. If the name is found only in the context of the entire postfix­expression, the name shall refer to a class­name or namespace­name. If the name is found in both contexts, the class­name­or­namespace­name shall refer to the same entity. [Note: the result of looking up the class­name­or­namespace­name is not required to be a unique base class of the class type of the object expression, as long as the entity or entities named by the qualified­id are members of the class type of the object expression and are not ambiguous according to 10.2.

        struct A {
            int a;
        };
        struct B: virtual A { };
        struct C: B { };
        struct D: B { };
        struct E: public C, public D { };
        struct F: public A { };

        void f() {
            E e;
            e.B::a = 0; //
    OK, only one A::a in E

            F f;
            f.A::a = 1; //
    OK, A::a is a member of F
        }

    --end note]

  6. If the qualified­id has the form

    ::class­name­or­namespace­name::...

    the class­name­or­namespace­name is looked up in global scope as a class­name or namespace­name.

  7. If the nested­name­specifier contains a class template­id (14.2), its template­arguments are evaluated in the context in which the entire postfix­expression occurs.
  8. If the id­expression is a conversion­function­id, its conversion­type­id shall denote the same type in both the context in which the entire postfix­expression occurs and in the context of the class of the object expression (or the class pointed to by the pointer expression).

3.4.6 Using­directives and namespace aliases

[basic.lookup.udir]

  1. When looking up a namespace­name in a using­directive or namespace­alias­definition, only namespace names are considered.

24) Appearing inside the braced­enclosed declaration­seq in a linkage­specification does not affect whether a declaration is a definition.

25) 8.3.6 describes how default argument names are looked up.

26) This refers to unqualified names following the function declarator; such a name may be used as a type or as a default argument name in the parameter­declaration­clause, or may be used in the function body.

27) This refers to unqualified names following the class name; such a name may be used in the base­clause or may be used in the class definition.

28) This lookup applies whether the definition of X is nested within Y's definition or whether X's definition appears in a namespace scope enclosing Y's definition (9.7).

29) That is, an unqualified name following the function declarator; such a name may be used as a type or as a default argument name in the parameter­declaration­clause, or may be used in the function body, or, if the function is a constructor, may be used in the expression of a mem­initializer.

30) This lookup applies whether the member function is defined within the definition of class X or whether the member function is defined in a namespace scope enclosing X's definition.