Inside the definition of a
(both
and
), the meaning of some constructs may differ from one instantiation to another. In particular, types and expressions may depend on types of type template parameters and values of non-type template parameters.
template<typenameT>structX:B<T>// “B<T>” is dependent on T{typenameT::A*pa;// “T::A” is dependent on T// (see below for the meaning of this use of “typename”)voidf(B<T>*pb){staticinti=B<T>::i;// “B<T>::i” is dependent on Tpb->j++;// “pb->j” is dependent on T}};Name
and binding are different for dependent names and non-dependent names.
Binding rules
Non-dependent names are looked up and bound at the point of template definition. This binding holds even if at the point of template instantiation there is a better match:
Run this code
#include<iostream>voidg(double){std::cout<<"g(double)\n";}template<classT>structS{voidf()const{g(1);// “g” is a non-dependent name, bound now}};voidg(int){std::cout<<"g(int)\n";}intmain(){g(1);// calls g(int)S<int>s;s.f();// calls g(double)}If the meaning of a non-dependent name changes between the definition context and the point of instantiation of a specialization of the template, the program is ill-formed, no diagnostic required. This is possible in the following situations:
a type used in a non-dependent name is
at the point of definition but complete at the point of instantiation
lookup for a name in the template definition found a
, but the lookup in the corresponding scope in the instantiation does not find any declarations because the using-declaration was a pack expansion and the corresponding pack is empty
(since C++17)an instantiation uses a default argument or default template argument that had not been defined at the point of definition
a
at the point of instantiation uses the value of a const object of integral or unscoped enum type, the value of a constexpr object, the value of a reference, or the definition of a constexpr function(since C++11), and that object/reference/function(since C++11) was not defined at the point of definition
the template uses a non-dependent class template specialization or variable template specialization(since C++14) at the point of instantiation, and this template it uses is either instantiated from a partial specialization that was not defined at the point of definition or names an explicit specialization that was not declared at the point of definition
Binding of dependent names is postponed until lookup takes place.
Lookup rules
The
of a dependent name used in a template is postponed until the template arguments are known, at which time
non-ADL lookup examines function declarations with external linkage that are visible from the template definition context
examines function declarations with external linkage that are visible from either the template definition context or the template instantiation context
(in other words, adding a new function declaration after template definition does not make it visible, except via ADL).
The purpose of this rule is to help guard against violations of the
for template instantiations:
// an external librarynamespaceE{template<typenameT>voidwriteObject(constT&t){std::cout<<"Value = "<<t<<'\n';}}// translation unit 1:// Programmer 1 wants to allow E::writeObject to work with vector<int>namespaceP1{std::ostream&operator<<(std::ostream&os,conststd::vector<int>&v){for(intn:v)os<<n<<' ';returnos;}voiddoSomething(){std::vector<int>v;E::writeObject(v);// Error: will not find P1::operator<<}}// translation unit 2:// Programmer 2 wants to allow E::writeObject to work with vector<int>namespaceP2{std::ostream&operator<<(std::ostream&os,conststd::vector<int>&v){for(intn:v)os<<n<<':';returnos<<"[]";}voiddoSomethingElse(){std::vector<int>v;E::writeObject(v);// Error: will not find P2::operator<<}}In the above example, if non-ADL lookup for operator<< were allowed from the instantiation context, the instantiation of E::writeObject<vector<int>> would have two different definitions: one using P1::operator<< and one using P2::operator<<. Such ODR violation may not be detected by the linker, leading to one or the other being used in both instances.
To make ADL examine a user-defined namespace, either
should be replaced by a user-defined class or its element type should be a user-defined class:
namespaceP1{// if C is a class defined in the P1 namespacestd::ostream&operator<<(std::ostream&os,conststd::vector<C>&v){for(Cn:v)os<<n;returnos;}voiddoSomething(){std::vector<C>v;E::writeObject(v);// OK: instantiates writeObject(std::vector<P1::C>)// which finds P1::operator<< via ADL}}Note: this rule makes it impractical to overload operators for standard library types:
Run this code
#include<iostream>#include<iterator>#include<utility>#include<vector>// Bad idea: operator in global namespace, but its arguments are in std::std::ostream&operator<<(std::ostream&os,std::pair<int,double>p){returnos<<p.first<<','<<p.second;}intmain(){typedefstd::pair<int,double>elem_t;std::vector<elem_t>v(10);std::cout<<v[0]<<'\n';// OK, ordinary lookup finds ::operator<<std::copy(v.begin(),v.end(),std::ostream_iterator<elem_t>(std::cout," "));// Error: both ordinary lookup from the point of definition of// std::ostream_iterator and ADL will only consider the std namespace,// and will find many overloads of std::operator<<, so the lookup will be done.// Overload resolution will then fail to find operator<< for elem_t// in the set found by the lookup.}Note: limited lookup (but not binding) of dependent names also takes place at template definition time, as needed to distinguish them from non-dependent names and also to determine whether they are members of the current instantiation or members of unknown specialization. The information obtained by this lookup can be used to detect errors, see below.
Dependent types
The following types are dependent types :
template parameter
a member of an unknown specialization (see below)
a nested class/enum that is a dependent member of unknown specialization (see below)
a cv-qualified version of a dependent type
a compound type constructed from a dependent type
an array type whose element type is dependent or whose bound (if any) is value-dependent
a function type whose parameters include one or more function
(since C++11)a function type whose exception specification is value-dependent
a
where either
the template name is a template parameter, or
any of template arguments is type-dependent, or value-dependent, or is a pack expansion(since C++11) (even if the template identifier is used without its argument list, as
)
the result of
applied to a type-dependent expression
The result of decltype applied to a type-dependent expression is a unique dependent type. Two such results refer to the same type only if their expressions are
.(since C++11)the
applied to a type-dependent constant expression
The pack indexing specifier applied to a type-dependent constant expression is a unique dependent type. Two such pack indexing specifiers refer to the same type only if their constant expressions are equivalent. Otherwise, two such pack indexing specifiers refer to the same type only if their indices have the same value.the result of
applied to a dependent constant expression
(since C++26)Note: a typedef member of a current instantiation is only dependent when the type it refers to is.
Type-dependent expressions
The following expressions are type-dependent :
an expression whose any subexpression is a type-dependent expression
this, if the class is a dependent type.
an
that is not a
and(since C++20) satisfies any of the following conditions:
contains an identifier for which name lookup finds at least one dependent declaration
contains a dependent
contains the name of
to a dependent type
contains a nested name specifier or
that is a member of unknown specialization
names a dependent member of the current instantiation which is a static data member of type “array of unknown bound”
contains the special identifier __func__ (if some enclosing function is a template, a non-template member of a class template, or a generic lambda(since C++14))
(since C++11)contains an identifier for which name lookup finds one or more declarations of member functions of the current instantiation declared with
(since C++14)contains an identifier for which name lookup finds a
structured binding declaration
whose initializer is type-dependent
contains an identifier for which name lookup finds a non-type template parameter whose type contains the placeholder auto
contains an identifier for which by name lookup finds a variable declared with a type that contains a
(e.g., auto static data member), where the initializer is type-dependent
(since C++17)contains an identifier for which name lookup finds a
contains a name introduced by the item-declaration of an
S, where the type specified for that name contains a
and either
S is not an
, or
the expansion-initializer of S is type-dependent
(since C++26)any cast expression to a dependent type
that creates an object of a dependent type
member access expression that refers to a member of the current instantiation whose type is dependent
member access expression that refers to a member of unknown specialization
The following expressions are never type-dependent because the types of these expressions cannot be:
pseudo-destructor calls
Value-dependent expressions
The following expressions are value-dependent :
an expression used in context where
is required, and whose any subexpression is value-dependent
an
that satisfies any of the following conditions:
It is type-dependent.
It is a name of a non-type template parameter.
It names a static data member that is a dependent member of the current instantiation and is not initialized.
It names a static member function that is a dependent member of the current instantiation.
It is a constant with a integer or enumeration(until C++11)literal(since C++11) type, initialized from a value-dependent expression.
It is a
and any of its arguments are dependent.
(since C++20)It is a name introduced by the item-declaration of an
.
(since C++26)the following expressions where the operand is a type-dependent expression:
the following expressions where the operand is a dependent type-id:
the following expressions where the target type is dependent or the operand is a type-dependent expression:
expression where the target type is dependent or a value-dependent expression is enclosed by parentheses or braces(since C++11)
address-of expression where the argument is a
that names a dependent member of the current instantiation
address-of expression where the argument is any expression which, evaluated as a core
, refers to a
that is an object with static or thread storage(since C++11) duration or a member function.
Dependent namespaces
names are dependent if they are introduced by
definitions that contain dependent
.
template<std::meta::infoR>intfn(){namespaceAlias=[:R:];// [:R:] is dependentreturntypenameAlias::T{};// Alias is dependent}namespaceNS{usingT=int;}inta=fn<^^NS>();(since C++26)Dependent names
Current instantiation
Within a class template definition (including its member functions and nested classes) some names may be deduced to refer to the current instantiation. This allows certain errors to be detected at the point of definition, rather than instantiation, and removes the requirement on the typename and template disambiguators for dependent names, see below.
Only the following names can refer to the current instantiation:
in the definition of a class template, a nested class of a class template, a member of a class template, or a member of a nested class of a class template: the injected-class-name of the class template or nested class
in the definition of a primary class template or a member of a primary class template: the name of the class template followed by template argument list (or an equivalent alias template specialization) for the primary template where each argument is equivalent (defined below) to its corresponding parameter.
in the definition of a nested class of a class template: the name of the nested class used as a member of the current instantiation
in the definition of a class template partial specialization or a member of a class template partial specialization: the name of the class template followed by template argument list for the partial specialization, where each argument is equivalent to its corresponding parameter
in the definition of a
: the name of a
A template argument is equivalent to a template parameter if
for a
, the template argument denotes the same type as the template parameter.
for a
, the template argument is an
that names a variable that is equivalent to the template parameter. A variable is equivalent to a template parameter if
it has the same type as the template parameter (ignoring cv-qualification) and
its initializer consists of a single identifier that names the template parameter or, recursively, such a variable.
template<classT>classA{A*p1;// A is the current instantiationA<T>*p2;// A<T> is the current instantiation::A<T>*p4;// ::A<T> is the current instantiationA<T*>p3;// A<T*> is not the current instantiationclassB{B*p1;// B is the current instantiationA<T>::B*p2;// A<T>::B is the current instantiationtypenameA<T*>::B*p3;// A<T*>::B is not the current instantiation};};template<classT>classA<T*>{A<T*>*p1;// A<T*> is the current instantiationA<T>*p2;// A<T> is not the current instantiation};template<intI>structB{staticconstintmy_I=I;staticconstintmy_I2=I+0;staticconstintmy_I3=my_I;staticconstlongmy_I4=I;staticconstintmy_I5=(I);B<my_I>*b1;// B<my_I> is the current instantiation:// my_I has the same type as I,// and it is initialized with only IB<my_I2>*b2;// B<my_I2> is not the current instantiation:// I + 0 is not a single identifierB<my_I3>*b3;// B<my_I3> is the current instantiation:// my_I3 has the same type as I,// and it is initialized with only my_I (which is equivalent to I)B<my_I4>*b4;// B<my_I4> is not the current instantiation:// the type of my_I4 (long) is not the same as the type of I (int)B<my_I5>*b5;// B<my_I5> is not the current instantiation:// (I) is not a single identifier};Note that a base class can be the current instantiation if a nested class derives from its enclosing class template. Base classes that are dependent types but are not the current instantiation are dependent base classes:
template<classT>structA{typedefintM;structB{typedefvoidM;structC;};};template<classT>structA<T>::B::C:A<T>{Mm;// OK, A<T>::M};A name is classified as a member of the current instantiation if it is
an unqualified name that is found by
in the current instantiation or in its non-dependent base.
, if the qualifier (the name to the left of ::) names the current instantiation and lookup finds the name in the current instantiation or in its non-dependent base
a name used in a class member access expression (y in x.y or xp->y), where the object expression (x or *xp) is the current instantiation and lookup finds the name in the current instantiation or in its non-dependent base
template<classT>classA{staticconstinti=5;intn1[i];// i refers to a member of the current instantiationintn2[A::i];// A::i refers to a member of the current instantiationintn3[A<T>::i];// A<T>::i refers to a member of the current instantiationintf();};template<classT>intA<T>::f(){returni;// i refers to a member of the current instantiation}Members of the current instantiation may be both dependent and non-dependent.
If the lookup of a member of current instantiation gives a different result between the point of instantiation and the point of definition, the lookup is ambiguous. Note however that when a member name is used, it is not automatically converted to a class member access expression, only explicit member access expressions indicate members of current instantiation:
structA{intm;};structB{intm;};template<typenameT>structC:A,T{intf(){returnthis->m;}// finds A::m in the template definition contextintg(){returnm;}// finds A::m in the template definition context};templateintC<B>::f();// error: finds both A::m and B::mtemplateintC<B>::g();// OK: transformation to class member access syntax// does not occur in the template definition contextUnknown specializations
Within a template definition, certain names are deduced to belong to an unknown specialization, in particular,
a
, if any name that appears to the left of :: is a dependent type that is not a member of the current instantiation
a
, whose qualifier is the current instantiation, and the name is not found in the current instantiation or any of its non-dependent base classes, and there is a dependent base class
a name of a member in a class member access expression (the y in x.y or xp->y), if the type of the object expression (x or *xp) is a dependent type and is not the current instantiation
a name of a member in a class member access expression (the y in x.y or xp->y), if the type of the object expression (x or *xp) is the current instantiation, and the name is not found in the current instantiation or any of its non-dependent base classes, and there is a dependent base class
template<typenameT>structBase{};template<typenameT>structDerived:Base<T>{voidf(){// Derived<T> refers to current instantiation// there is no “unknown_type” in the current instantiation// but there is a dependent base (Base<T>)// Therefore, “unknown_type” is a member of unknown specializationtypenameDerived<T>::unknown_typez;}};template<>structBase<int>// this specialization provides it{typedefintunknown_type;};
This classification allows the following errors to be detected at the point of template definition (rather than instantiation):
If any template definition has a
in which the qualifier refers to the current instantiation and the name is neither a member of current instantiation nor a member of unknown specialization, the program is ill-formed (no diagnostic required) even if the template is never instantiated.
template<classT>classA{typedefinttype;voidf(){A<T>::typei;// OK: “type” is a member of the current instantiationtypenameA<T>::otherj;// Error:// “other” is not a member of the current instantiation// and it is not a member of an unknown specialization// because A<T> (which names the current instantiation),// has no dependent bases for “other” to hide in.}};If any template definition has a member access expression where the object expression is the current instantiation, but the name is neither a member of current instantiation nor a member of unknown specialization, the program is ill-formed even if the template is never instantiated.
Members of unknown specialization are always dependent, and are looked up and bound at the point of instantiation as all dependent names (see above)
The typename disambiguator for dependent names
In a declaration or a definition of a template, including alias template, a name that is not a member of the current instantiation and is dependent on a template parameter is not considered to be a type unless the keyword typename is used or unless it was already established as a type name, e.g. with a typedef declaration or by being used to name a base class.
Run this code
#include<iostream>#include<vector>intp=1;template<typenameT>voidfoo(conststd::vector<T>&v){// std::vector<T>::const_iterator is a dependent name,typenamestd::vector<T>::const_iteratorit=v.begin();// without “typename”, the following is parsed as multiplication// of the type-dependent data member “const_iterator”// and some variable “p”. Since there is a global “p” visible// at this point, this template definition compiles.std::vector<T>::const_iterator*p;typedeftypenamestd::vector<T>::const_iteratoriter_t;iter_t*p2;// “iter_t” is a dependent name, but it is known to be a type name}template<typenameT>structS{typedefintvalue_t;// member of current instantiationvoidf(){S<T>::value_tn{};// S<T> is dependent, but “typename” not neededstd::cout<<n<<'\n';}};intmain(){std::vector<int>v;foo(v);// template instantiation fails: there is no member variable// called “const_iterator” in the type std::vector<int>S<int>().f();}The keyword typename may only be used in this way before qualified names (e.g. T::x), but the names need not be dependent.
Usual
is used for the identifier prefixed by typename. Unlike the case with
, the lookup rules do not change despite the qualifier:
structA// A has a nested variable X and a nested type struct X{structX{};intX;};structB{structX{};// B has a nested type struct X};template<classT>voidf(Tt){typenameT::Xx;}voidfoo(){Aa;Bb;f(b);// OK: instantiates f<B>, T::X refers to B::Xf(a);// error: cannot instantiate f<A>:// because qualified name lookup for A::X finds the data member}The keyword typename can be used even outside of templates.
#include<vector>intmain(){// Both OK (after resolving CWG 382)typedeftypenamestd::vector<int>::const_iteratoriter_t;typenamestd::vector<int>v;}The typename keyword can also be used in a
to indicate that it designates a type.
(since C++26)In some contexts, only type names can validly appear. In these contexts, a dependent qualified name or a splice specifier(since C++26) is assumed to name a type and no typename is required:
A qualified name that is used as a
in the (top-level) decl-specifier-seq of:
a
or
at namespace scope;
a
;
a
in a
(including friend function declarations), outside of default arguments;
a
of a
declarator for a function or function template
whose name is qualified, outside of default arguments;
a
of a
outside of default arguments;
a parameter declaration of a
;
the type in the declaration of a
;
A qualified name that appears in
, where the smallest enclosing type-id is:
the type in a
that does not parenthesize its type;
the type-id in an
;
a
,
a
default argument of a type template parameter
, or
the type-id of a
,
,
, or
.
(since C++20)The template disambiguator for dependent names
Similarly, in a template definition, a dependent name that is not a member of the current instantiation is not considered to be a template name unless the disambiguation keyword template is used or unless it was already established as a template name:
Run this code
template<typenameT>structS{template<typenameU>voidfoo(){}};template<typenameT>voidbar(){S<T>s;s.foo<T>();// error: < parsed as less than operators.templatefoo<T>();// OK}The keyword template may only be used in this way after operators :: (scope resolution), -> (member access through pointer), and . (member access), the following are all valid examples:
T::templatefoo<X>();
s.templatefoo<X>();
this->templatefoo<X>();
typenameT::templateiterator<int>::value_typev;
As is the case with typename, the template prefix is allowed even if the name is not dependent or the use does not appear in the scope of a template.
Even if the name to the left of :: refers to a namespace, the template disambiguator is allowed:
template<typename>structS{};::templateS<void>q;// allowed, but unnecessaryDue to the special rules for
for template names in member access expressions, when a non-dependent template name appears in a member access expression (after -> or after .), the disambiguator is unnecessary if there is a class or alias(since C++11) template with the same name found by ordinary lookup in the context of the expression. However, if the template found by lookup in the context of the expression differs from the one found in the context of the class, the program is ill-formed(until C++11)
template<int>structA{intvalue;};template<classT>voidf(Tt){t.A<0>::value;// Ordinary lookup of A finds a class template.// A<0>::value names member of class A<0>// t.A < 0; // Error: “<” is treated as the start of template argument list}(until C++23)The template keyword can also be used in a
to indicate that it designates a template.
(since C++26)Keywords
,
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++98 it was unspecified at what point semantic constraints are
applied when a type used in a non-dependent name is
incomplete at the point at which a template is defined but is
complete at the point at which an instantiation is performed the program is ill-formed
and no diagnostic is
required in this case
C++98 the definition of dependent types was based
on the form of the name rather than lookup definition revamped
C++98 the typename disambiguator was only allowed in template scope also allowed outside
of templates
C++98 the template disambiguator was only allowed in template scope also allowed outside
of templates
C++98 it was unspecified whether nested enumerations are dependent dependent as nested classes
C++98 typeid expressions were never value-dependent value-dependent if the
operand is type-dependent
C++98 it was unspecified whether a name refers to the current instantiation
when a template identifier matching a primary template or partial
specialization appears in the definition of a member of the template specified
C++98 uninitialized static data member, static member function, and address
of member of a class template were not listed as value-dependent listed
C++98 a nested type of a non-dependent base of
the current instantiation was dependent it is not dependent
C++98 the list of cases that meaning may change between the
definition context and the point of instantiation was incomplete made complete
C++98 it was not clear whether the template disambiguator can
follow a :: where the name to its left refers to a namespace allowed
C++98 this was never value-dependent it may be
value-dependent
C++98 address of a static data member of class
template was not listed as value-dependent listed
C++98 type-dependent identifier expressions might not be value-dependent they are always
value-dependent
C++98 a function type whose exception specification
is value-dependent was not a dependent type it is
C++98 a parenthesized non-type template parameter used as a
template argument was equivalent to that template parameter not equivalent anymore
C++11 a function type with function parameter
pack was not a dependent type it is
C++20 requires expressions might be type-dependent they are never
type-dependent
C++11 a noexcept expression was only value-dependent
if its operand is value-dependent it is value-dependent
if its operand involves
a template parameter
C++98 the names of local classes of templated
functions were not part of the current instantiation they are