-
[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.
-
This clause does not cover concepts that affect only a single part of the language.
Such concepts are discussed in the relevant clauses.]
-
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.
-
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.
-
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).
-
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).
-
Two names are the same if
-
they are identifiers composed of the same character sequence; or
-
they are the names of overloaded operator functions formed with the same operator; or
-
they are the names of userdefined conversion functions formed with the same type.
-
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] |
-
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.
-
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 linkagespecification
24)
(7.5) and neither an initializer nor a functionbody, 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 usingdeclaration (7.3.3), or a usingdirective (7.3.4).
-
[Example: all but one of the following are definitions:
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]
-
[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
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]
-
[Note: a class name can also be implicitly declared by an elaboratedtypespecifier (3.3.1).]
-
A program is illformed if the definition of any object gives the object an incomplete type (3.9).
3.2 One definition rule |
[basic.def.odr] |
-
No translation unit shall contain more than one definition of any variable, function, class type, enumeration type or template.
-
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 nonoverloaded function is used if its name appears in a potentiallyevaluated 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 potentiallyevaluated expression.
[Note: this covers calls to named functions (5.2.2), operator overloading (clause 13), userdefined conversions (12.3.2), allocation function for placement new (5.3.4), as well as nondefault 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 potentiallyevaluated 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 potentiallyevaluated expression as specified in 5.3.5 and 12.5.
A copyassignment function for a class is used by an implicitlydefined copyassignment 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.
-
Every program shall contain exactly one definition of every noninline 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 userdefined 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.
-
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 wellformed, even though it never defines X:
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:
-
an object of type T is defined (3.1, 5.3.4), or
-
an lvaluetorvalue conversion is applied to an lvalue referring to an object of type T (4.1), or
-
an expression is converted (either implicitly or explicitly) to type T (clause 4, 5.2.3, 5.2.7, 5.2.9, 5.4), or
-
an expression that is not a null pointer constant, and has type other than void *, is converted to the type pointer to T or reference to T using an implicit conversion (clause 4), a dynamic_cast (5.2.7) or a static_cast (5.2.9), or
-
a class member access operator is applied to an expression of type T (5.2.5), or
-
the typeid operator (5.2.8) or the sizeof operator (5.3.3) is applied to an operand of type T, or
-
a function with a return type or argument type of type T is defined (3.1) or called (5.2.2), or
-
an lvalue of type T is assigned to (5.17).
-
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), nonstatic 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
-
each definition of D shall consist of the same sequence of tokens; and
- in each definition of D, corresponding names, looked up according to 3.4, shall refer to an entity defined within the definition of D, or shall refer to the same entity, after overload resolution (13.3) and after matching of partial template specialization (14.8.3), except that a name can refer to a const object with internal or no linkage if the object has the same integral or enumeration type in all definitions of D,and the object is initialized with a constant expression (5.19), and the value (but not the address) of the object is used, and the object has the same value in all definitions of D; and
- in each definition of D, the overloaded operators referred to, the implicit calls to conversion functions, constructors, operator new functions and operator delete functions, shall refer to the same function, or to a function defined within the definition of D; and
-
in each definition of D, a default argument used by an (implicit or explicit) function call is treated as if its token sequence were present in the definition of D; that is, the default argument is subject to the three requirements described above (and, if the default argument has subexpressions with default arguments, this requirement applies recursively). 25)
-
if D is a class with an implicitlydeclared constructor (12.1), it is as if the constructor was implicitly defined in every translation unit where it is used, and the implicit definition in every translation unit shall call the same constructor for a base class or a class member of D.
[Example:
// translation unit 1:
struct X {
X (int);
X (int, int);
};
X::X (int = 0) { }
class D: public X { };
D d2;
// X (int) called by D ()
// translation unit 2:
struct X {
X (int);
X (int, int);
};
X::X (int = 0, int = 0) { }
class D: public X { };
// X (int, int) called by D ();
// D () 's implicit definition
// violates the ODR
end example] If D is a template, and is defined in more than one translation unit, then the last four requirements from the list above shall apply to names from the template's enclosing scope used in the template definition (14.6.3), and also to dependent names at the point of instantiation (14.6.2).
If the definitions of D satisfy all these requirements, then the program shall behave as if there were a single definition of D.
If the definitions of D do not satisfy these requirements, then the behavior is undefined.
3.2 Declarative regions and scopes |
[basic.scope] |
-
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.
-
[Example: in
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.]
-
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 elaboratedtypespecifier (3.3.1), and usingdirectives (7.3.4) alter this general behavior.
-
Given a set of declarations in a single declarative region, each of which specifies the same unqualified name,
-
they shall all refer to the same entity, or all refer to functions and function templates; or
-
exactly one declaration shall declare a class name or enumeration name that is not a typedef name and the other declarations shall all refer to the same object or enumerator, or all refer to functions and function templates; in this case the class name or enumeration name is hidden (3.3.7).
[Note: a namespace name or a class template name must be unique in its declarative region (7.3.2, clause 14).]
[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, elaboratedtypespecifiers (3.3.1) and friend declarations (11.4) may introduce a (possibly not visible) name into an enclosing namespace; 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.]
-
[Note: the name lookup rules are summarized in 3.4.]
3.3.1 Point of declaration |
[basic.scope.pdecl] |
-
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:
int x = 12;
{ int x = x; }
Here the second x is initialized with its own (indeterminate) value.]
-
[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.]]
-
The point of declaration for an enumerator is immediately after its enumeratordefinition.
[Example:
const int x = 12;
{ enum { x = x }; }
Here, the enumerator x is initialized with the value of the constant x, namely 12.]
-
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]
-
The point of declaration of a class first declared in an elaboratedtypespecifier is as follows:
-
for an elaboratedtypespecifier of the form
classkey identifier ;
the elaboratedtypespecifier declares the identifier to be a classname in the scope that ontains the declaration, otherwise
-
for an elaboratedtypespecifier of the form
classkey identifier
if the elaboratedtypespecifier is used in the declspecifierseq or parameterdeclarationclause of a function defined in namespace scope, the identifier is declared as a classname in the namespace that contains the declaration; otherwise, except as a friend declaration, the identifier is declared in the smallest nonclass, nonfunctionprototype scope that contains the declaration. [Note: if the elaboratedtypespecifier designates an enumeration, the identifier must refer to an already declared enumname.
If the identifier in the elaboratedtypespecifier is a qualifiedid, it must refer to an already declared classname or enumname. See 3.4.4.]
-
[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.]
-
[Note: For point of instantiation of a template, see 14.7.1.]
3.3.2 Local scope |
[basic.scope.local] |
-
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.
-
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 tryblock 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 tryblock.
-
The name in a catch exceptiondeclaration is local to the handler and shall not be redeclared in the outermost block of the
handler.
-
Names declared in the forinitstatement, 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] |
-
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.
3.3.4 Function scope |
[basic.funscope] |
-
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] |
-
The declarative region of a namespacedefinition is its namespacebody.
The potential scope denoted by an originalnamespacename is the concatenation of the declarative regions established by each of the namespacedefinitions in the same declarative region with that originalnamespacename.
Entities declared in a namespacebody 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 usingdirective (7.3.4) that nominates the member's namespace, the member's potential scope includes that portion of the potential scope of the usingdirective 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]
-
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 usingdirective; see 3.4.3.2.
-
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] |
-
The following rules describe the scope of names declared in classes.
-
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).
-
A name N used in a class S shall refer to the same declaration in its context and when reevaluated in the completed scope of S.
No diagnostic is required for a violation of this rule.
-
If reordering member declarations in a class yields an alternate valid program under (1) and (2), the program is illformed, no diagnostic is required.
-
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.
-
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 ctorinitializer (12.6.2)) and any portion of the declarator part of such definitions which follows the identifier, including a parameterdeclarationclause 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]
-
The name of a class member shall only be used as follows:
-
in the scope of its class (as described above) or a class derived (clause 10) from its class,
-
after the . operator applied to an expression of the type of its class (5.2.5) or a class derived from its class,
-
after the > operator applied to a pointer to an object of its class (5.2.5) or a class derived from its class,
-
after the :: scope resolution operator (5.1) applied to the name of its class or a class derived from its class.
3.3.7 Name hiding |
[basic.scope.hiding] |
-
A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class (10.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.
-
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.
-
During the lookup of a name qualified by a namespace name, declarations that would otherwise be made visible by a usingdirective can be hidden by declarations with the same name in the namespace containing the usingdirective; see (3.4.3.2).
-
If a name is in scope and is not hidden it is said to be visible.
3.4 Name lookup |
[basic.lookup] |
-
The name lookup rules apply uniformly to all names (including typedefnames (7.1.3), namespacenames (7.3) and classnames (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).
-
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.
-
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.
-
[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] |
-
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 illformed.
-
The declarations from the namespace nominated by a usingdirective become visible in a namespace enclosing the usingdirective; 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 usingdirective are considered members of that enclosing namespace.
-
The lookup for an unqualified name used as the postfixexpression of a function call is described in 3.4.2.
[Note: for purposes of determining (during parsing) whether an expression is a postfixexpression 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 castexpression equivalent to int (a).
Because the expression is not a function call, the argumentdependent name lookup (3.4.2) does not apply and the friend function f is not found.]
-
A name used in global scope, outside of any function, class or userdeclared namespace, shall be declared before its use in global scope.
-
A name used in a userdeclared 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.
-
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]
-
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:
-
before its use in class X or be a member of a base class of X (10.2), or
-
if X is a nested class of class Y (9.7), before the definition of X in Y, or shall be a member of a base class of Y (this lookup applies in turn to Y's enclosing classes, starting with the innermost enclosing class),
28)
or
-
if X is a local class (9.8) or is a nested class of a local class, before the definition of class X in a block enclosing the definition of class X, or
-
if X is a member of namespace N, or is a nested class of a class that is a member of N, or is a local class or a nested class within a local class of a function that is a member of N, before the definition of class X in namespace N or in one of N's enclosing namespaces.
[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.]
-
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:
-
before its use in the block in which it is used or in an enclosing block (6.3), or
-
shall be a member of class X or be a member of a base class of X (10.2), or
-
if X is a nested class of class Y (9.7), shall be a member of Y, or shall be a member of a base class of Y (this lookup applies in turn to Y's enclosing classes, starting with the innermost enclosing class),
30)
or
-
if X is a local class (9.8) or is a nested class of a local class, before the definition of class X in a block enclosing the definition of class X, or
-
if X is a member of namespace N, or is a nested class of a class that is a member of N, or is a local class or a nested class within a local class of a function that is a member of N, before the member function definition, in namespace N or in one of N's enclosing namespaces.
[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
--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.]
-
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.
-
In a friend declaration naming a member function, a name used in the function declarator and not part of a templateargument in a templateid 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 templateargument in a templateid, 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]
-
During the lookup for a name used as a default argument (8.3.6) in a function parameterdeclarationclause or used in the expression of a meminitializer 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 ctorinitializer.]
-
A name used in the definition of a static data member of class X (9.4.2) (after the qualifiedid 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.]
-
A name used in the handler for a functiontryblock (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 exceptiondeclaration nor in the outermost block of a handler for the functiontryblock.
Names declared in the outermost block of the function definition are not found when looked up in the scope of a handler for the functiontryblock.
[Note: but function parameter names are found.]
-
[Note: the rules for name lookup in template definitions are described in 14.6.]
3.4.2 Argumentdependent name lookup |
[basic.lookup.koenig] |
-
When an unqualified name is used as the postfixexpression in a function call (5.2.2), other namespaces not considered during the usual unqualified lookup (3.4.1) may be searched, and namespacescope friend function 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).
-
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 usingdeclarations used to specify the types do not contribute to this set.
The sets of namespaces and classes are determined in the following way:
-
If T is a fundamental type, its associated sets of namespaces and classes are both empty.
-
If T is a class type, its associated classes are the class itself and its direct and indirect base classes.
Its associated namespaces are the namespaces in which its associated classes are defined.
-
If T is a union or enumeration type, its associated namespace is the namespace in which it is defined.
If it is a class member, its associated class is the member's class; else it has no associated class.
-
If T is a pointer to U or an array of U,its associated namespaces and classes are those associated with U.
-
If T is a function type, its associated namespaces and classes are those associated with the function parameter types and those associated with the return type.
-
If T is a pointer to a member function of a class X,its associated namespaces and classes are those associated with the function parameter types and return type, together with those associated with X.
-
If T is a pointer to a data member of class X, its associated namespaces and classes are those associated with the member type together with those associated with X.
-
If T is a templateid, its associated namespaces and classes are the namespace in which the template is defined; for member templates, the member template's class; the namespaces and classes associated with the types of the template arguments provided for template type parameters (excluding template template parameters); the namespaces in which any template template arguments are defined; and the classes in which any member templates used as template template arguments are defined.
[Note: nontype template arguments do not contribute to the set of associated namespaces.]
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]
-
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:
-
Any usingdirectives in the associated namespace are ignored.
-
Any namespacescope friend functions declared in associated classes are visible within their respectivenamespaces even if they are not visible during an ordinary lookup (11.4).
3.4.3 Qualified name lookup |
[basic.lookup.qual] |
-
The name of a class or namespace member can be referred to after the :: scope resolution operator (5.1) applied to a nestednamespecifier 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 classname (clause 9) or namespacename (7.3.1), the program is illformed.
[Example:
class A {
public :
static int n;
};
int main()
{
int A;
A::n = 42; // OK
A b; // illformed: A does not name a type
}
--end example]
-
[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.]
-
In a declaration in which the declaratorid is a qualifiedid, names used before the qualifiedid being declared are looked up in the defining namespace scope; names following
the qualifiedid 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]; // illformed:
// equivalent to: ::X C::arr [C::number];
// not to: C::X C::arr [C::number];
--end example]
-
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 usingdirective (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).
-
If a pseudodestructorname (5.2.4) contains a nestednamespecifier, the typenames are looked up as types in the scope designated by the nestednamespecifier.
In a qualifiedid of the form:
::opt nestednamespecifier ~ classname
where the nestednamespecifier designates a namespace scope, and in a qualifiedid of the form:
::opt nestednamespecifier classname::~ classname
the classnames are looked up as types in the scope designated by the nestednamespecifier.
[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 postfixexpression
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.]
3.4.3.1 Class members |
[class.qual] |
-
If the nestednamespecifier of a qualifiedid nominates a class, the name specified after the nestednamespecifier is looked up in the scope of the class (10.2), except for the cases listed below.
The name shall represent 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 qualifiedid at any point in its potential scope (3.3.6).]
The exceptions to the name lookup rule above are the following:
-
a destructor name is looked up as specified in 3.4.3;
-
conversiontypeid of an operatorfunctionid is looked up both in the scope of the class and in the context in which the entire postfixexpression occurs and shall refer to the same type in both contexts;
-
the templatearguments of a templateid are
looked up in the context in which the entire postfixexpression occurs.
-
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] |
-
If the nestednamespecifier of a qualifiedid nominates a namespace, the name specified after the nestednamespecifier is looked up in the scope of the namespace, except that the templatearguments of a templateid are looked up in the context in which the entire postfixexpression occurs.
-
Given X::m (where X is a userdeclared 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 usingdirectives in X and its used namespaces, except that usingdirectives 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 illformed.
Otherwise, if S has exactly one member, or if the context of the reference is a usingdeclaration (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 illformed.
[Example:
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 illformed
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 illformed
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)
}
-
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;
}
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 }
}
-
Because each referenced namespace is searched at most once, the following is welldefined:
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]
-
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 nontype 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 illformed. [Example:
namespace A {
struct x { };
int x;
int y;
}
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]
-
In a declaration for a namespace member in which the declaratorid is a qualifiedid, given that the qualifiedid for the namespace member has the form
nestednamespecifier unqualifiedid
the unqualifiedid shall name a member of the namespace designated by the nestednamespecifier.
[Example:
namespace A {
namespace B {
void f1 (int);
}
using namespace B;
}
void A::f1 (int) { } // illformed, f1 is not a member of A
--end example] However, in such namespace member declarations, the nestednamespecifier may rely on usingdirectives to implicitly provide the initial part of the nestednamespecifier.
[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] |
-
An elaboratedtypespecifier may be used to refer to a previously declared classname or enumname even though the name has been hidden by a nontype declaration (3.3.7).
The classname or enumname in the elaboratedtypespecifier may either be a simple identifer or be a qualifiedid.
-
If the name in the elaboratedtypespecifier is a simple identifer, and unless the elaboratedtypespecifier has the following form:
classkey identifier ;
the identifier is looked up according to 3.4.1 but ignoring any nontype names that have been declared.
If this name lookup finds a typedefname, the elaboratedtypespecifier is illformed.
If the elaboratedtypespecifier refers to an enumname and this lookup does not find a previously declared enumname, the
elaboratedtypespecifier is illformed.
If the elaboratedtypespecifier refers to an classname and this lookup does not find a previously declared classname, or if the elaboratedtypespecifier has the form:
classkey identifier ;
the elaboratedtypespecifier is a declaration that introduces the classname as described in 3.3.1.
-
If the name is a qualifiedid, the name is looked up according its qualifications, as described in 3.4.3, but ignoring any nontype names that have been declared.
If this name lookup finds a typedefname, the elaboratedtypespecifier is illformed.
If this name lookup does not find a previously declared classname or enumname, the elaboratedtypespecifieris illformed.
[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] |
-
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 lessthan 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 postfixexpression 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 postfixexpression 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 postfixexpression 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 illformed.
-
If the idexpression in a class member access (5.2.5) is an unqualifiedid, and the type of the object expression is of a class type C (or of pointer to a class type C), the unqualifiedid is looked up in the scope of class C.
If the type of the object expression is of pointer to scalar type, the unqualifiedid is looked up in the context of the complete postfixexpression.
-
If the unqualifiedid is ~typename, and the type of the object expression is of a class type C (or of pointer to a class type C), the typename is looked up in the context of the entire postfixexpression and in the scope of class C.
The typename shall refer to a classname.
If typename 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 typename is looked up in the scope of the complete postfixexpression.
-
If the idexpression in a class member access is a qualified
id of the form classnameornamespacename::...
the classnameornamespacename following the . or > operator is looked up both in the context of the entire postfixexpression 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 classname.
If the name is found only in the context of the entire postfixexpression, the name shall refer to a classname or namespacename.
If the name is found in both contexts, the classnameornamespacename shall refer to the same entity.
[Note: the result of looking up the classnameornamespacename 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 qualifiedid 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]
-
If the qualifiedid has the form
::classnameornamespacename::...
the classnameornamespacename is looked up in global scope as a classname or namespacename.
-
If the nestednamespecifier contains a class templateid (14.2), its templatearguments are evaluated in the context in which the entire postfixexpression occurs.
-
If the idexpression is a conversionfunctionid, its conversiontypeid shall denote the same type in both the context in which the entire postfixexpression occurs and in the context of the class of the object expression (or the class pointed to by the pointer expression).
3.4.6 Usingdirectives and namespace aliases |
[basic.lookup.udir] |
-
When looking up a namespacename in a usingdirective or namespacealiasdefinition, only namespace names are considered.
24)
Appearing inside the bracedenclosed declarationseq in a linkagespecification 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 parameterdeclarationclause, 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 baseclause 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 parameterdeclarationclause, or may be used in the function body, or, if the function is a constructor, may be used in the expression of a meminitializer.
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.