Function template - cppreference.com

A function template defines a family of functions.

Syntax

template<parameter-list>function-declaration(1) template<parameter-list>requiresconstraintfunction-declaration(2) (since C++20)function-declaration-with-placeholders(3) (since C++20)exporttemplate<parameter-list>function-declaration(4) (removed in C++11)Explanation

parameter-list- a non-empty comma-separated list of the

template parameters

, each of which is either

non-type parameter

, a

type parameter

, a

template parameter

, or a

parameter pack

of any of those(since C++11). As with any template, parameters may be

constrained

(since C++20)function-declaration- a

function declaration

. The function name declared becomes a template name. constraint- a

constraint expression

which restricts the template parameters accepted by this function template function-declaration-
with-placeholders- a

function declaration

where the type of at least one parameter uses the placeholder

auto

or

Concept auto

: the template parameter list will have one invented parameter for each placeholder (see Abbreviated function templates below) export was an optional modifier which declared the template as exported (when used with a class template, it declared all of its members exported as well). Files that instantiated exported templates did not need to include their definitions: the declaration was sufficient. Implementations of export were rare and disagreed with each other on details.

(until C++11)Abbreviated function template

When placeholder types (either

auto

or

Concept auto

) appear in the parameter list of a function declaration or of a function template declaration, the declaration declares a function template, and one invented template parameter for each placeholder is appended to the template parameter list:

voidf1(auto);// same as template<class T> void f1(T)voidf2(C1auto);// same as template<C1 T> void f2(T), if C1 is a conceptvoidf3(C2auto...);// same as template<C2... Ts> void f3(Ts...), if C2 is a conceptvoidf4(constC3auto*,C4auto&);// same as template<C3 T, C4 U> void f4(const T*, U&);template<classT,CU>voidg(Tx,Uy,Cautoz);// same as template<class T, C U, C W> void g(T x, U y, W z);Abbreviated function templates can be specialized like all function templates.

template<>voidf4<int>(constint*,constdouble&);// specialization of f4<int, const double>(since C++20)Function template signature

Every function template has a signature.

The signature of a template-head is the

template parameter list

, excluding template parameter names and

default arguments

, and requires-clause (if any)(since C++20).

The signature of a function template contains the name, parameter-type-list, return type, trailing requires-clause (if any)(since C++20), and signature of the template-head. Except for the following cases, its signature also contains the enclosing namespace.

If the function template is a class member, its signature contains the class of which the function is a member instead of the enclosing namespace. Its signature also contains the trailing requires-clause (if any)(since C++20), ref-qualifier (if any), and(since C++11)cv-qualifiers (if any).

If the function template is a

friend

with constraint involving enclosing template parameters, its signature contains the enclosing class instead of the enclosing namespace.

(since C++20)Function template instantiation

A function template by itself is not a type, or a function. No code is generated from a source file that contains only template definitions. In order for any code to appear, a template must be instantiated: the template arguments must be determined so that the compiler can generate an actual function (or class, from a class template).

Explicit instantiation

templatereturn-typename<argument-list>(parameter-list);(1) templatereturn-typename(parameter-list);(2) externtemplatereturn-typename<argument-list>(parameter-list);(3) (since C++11)externtemplatereturn-typename(parameter-list);(4) (since C++11)1) Explicit instantiation definition (without

template argument deduction

if every non-default template parameter is explicitly specified)

2) Explicit instantiation definition with template argument deduction for all parameters

3) Explicit instantiation declaration (without template argument deduction if every non-default template parameter is explicitly specified)

4) Explicit instantiation declaration with template argument deduction for all parameters

An explicit instantiation definition forces instantiation of the function or member function they refer to. It may appear in the program anywhere after the template definition, and for a given argument-list, is only allowed to appear once in the program, no diagnostic required.

An explicit instantiation declaration (an extern template) prevents implicit instantiations: the code that would otherwise cause an implicit instantiation has to use the explicit instantiation definition provided somewhere else in the program.

(since C++11)A trailing template-argument can be left unspecified in an explicit instantiation of a function template specialization or of a member function template specialization if it can be

deduced

from the function parameter:

template<typenameT>voidf(Ts){std::cout<<s<<'\n';}templatevoidf<double>(double);// instantiates f<double>(double)templatevoidf<>(char);// instantiates f<char>(char), template argument deducedtemplatevoidf(int);// instantiates f<int>(int), template argument deducedExplicit instantiation of a function template or of a member function of a class template cannot use inline or constexpr. If the declaration of the explicit instantiation names an implicitly-declared special member function, the program is ill-formed.

Explicit instantiation of a

constructor

cannot use a template parameter list (syntax (1)), which is also never necessary because they can be deduced (syntax (2)).

Explicit instantiation of a

prospective destructor

must name the selected destructor of the class.

(since C++20)Explicit instantiation declarations do not suppress the implicit instantiation of

inline

functions,

auto

-declarations, references, and class template specializations. (thus, when the inline function that is a subject of explicit instantiation declaration is ODR-used, it is implicitly instantiated for inlining, but its out-of-line copy is not generated in this translation unit)

Explicit instantiation definition of a function template with

default arguments

is not a use of the arguments, and does not attempt to initialize them:

char*p=0;template<classT>Tg(Tx=&p){returnx;}templateintg<int>(int);// OK even though &p isn’t an int.Implicit instantiation

When code refers to a function in context that requires

the function definition to exist

, or if the existence of the definition affects the semantics of the program(since C++11), and this particular function has not been explicitly instantiated, implicit instantiation occurs. The list of template arguments does not have to be supplied if it can be

deduced

from context.

Run this code

#include<iostream>template<typenameT>voidf(Ts){std::cout<<s<<'\n';}intmain(){f<double>(1);// instantiates and calls f<double>(double)f<>('a');// instantiates and calls f<char>(char)f(7);// instantiates and calls f<int>(int)void(*pf)(std::string)=f;// instantiates f<string>(string)pf("∇");// calls f<string>(string)}The existence of a definition of function is considered to affect the semantics of the program if the function is

needed for constant evaluation

by an expression, even if constant evaluation of the expression is not required or if constant expression evaluation does not use the definition.

template<typenameT>constexprintf(){returnT::value;}template<boolB,typenameT>voidg(decltype(B?f<T>():0));template<boolB,typenameT>voidg(...);template<boolB,typenameT>voidh(decltype(int{B?f<T>():0}));template<boolB,typenameT>voidh(...);voidx(){g<false,int>(0);// OK: B ? f<T>() : 0 is not potentially constant evaluatedh<false,int>(0);// error: instantiates f<int> even though B evaluates to false// and list-initialization of int from int cannot be narrowing}(since C++11)Note: omitting <> entirely allows

overload resolution

to examine both template and non-template overloads.

Template argument deduction

In order to instantiate a function template, every template argument must be known, but not every template argument has to be specified. When possible, the compiler will deduce the missing template arguments from the function arguments. This occurs when a function call is attempted and when an address of a function template is taken.

template<typenameTo,typenameFrom>Toconvert(Fromf);voidg(doubled){inti=convert<int>(d);// calls convert<int,double>(double)charc=convert<char>(d);// calls convert<char,double>(double)int(*ptr)(float)=convert;// instantiates convert<int, float>(float)}This mechanism makes it possible to use template operators, since there is no syntax to specify template arguments for an operator other than by re-writing it as a function call expression.

#include<iostream>intmain(){std::cout<<"Hello, world"<<std::endl;// operator<< is looked up via ADL as std::operator<<,// then deduced to operator<<<char, std::char_traits<char>> both times// std::endl is deduced to &std::endl<char, std::char_traits<char>>}Template argument deduction takes place after the function template

name lookup

(which may involve

argument-dependent lookup

) and before

overload resolution

.

See

template argument deduction

for details.

Explicit template arguments

Template arguments of a function template may be obtained from

template argument deduction

default template arguments

specified explicitly, which can be done in the following contexts:

in a function call expression

when an address of a function is taken

when a reference to function is initialized

when a pointer to member function is formed

in an explicit specialization

in an explicit instantiation

in a friend declaration

There is no way to explicitly specify template arguments to

overloaded operators

,

conversion functions

, and constructors, because they are called without the use of the function name.

The specified template arguments must match the template parameters in kind (i.e., type for type, non-type for non-type, and template for template). There cannot be more arguments than there are parameters (unless one parameter is a parameter pack, in which case there has to be an argument for each non-pack parameter)(since C++11).

The specified non-type arguments must either match the types of the corresponding non-type template parameters, or be

convertible to them

.

The function parameters that do not participate in template argument deduction (e.g. if the corresponding template arguments are explicitly specified) are subject to implicit conversions to the type of the corresponding function parameter (as in the usual

overload resolution

).

A template parameter pack that is explicitly specified may be extended by template argument deduction if there are additional arguments:

template<class...Types>voidf(Types...values);voidg(){f<int*,float*>(0,0,0);// Types = {int*, float*, int}}(since C++11)Template argument substitution

When all template arguments have been specified, deduced or obtained from default template arguments, every use of a template parameter in the function parameter list is replaced with the corresponding template arguments.

Substitution failure (that is, failure to replace template parameters with the deduced or provided template arguments) of a function template removes the function template from the

overload set

. This allows a number of ways to manipulate overload sets using template metaprogramming: see

SFINAE

for details.

After substitution, all function parameters of array and function type are adjusted to pointers and all top-level cv-qualifiers are dropped from function parameters (as in a regular

function declaration

).

The removal of the top-level cv-qualifiers does not affect the type of the parameter as it appears within the function:

template<classT>voidf(Tt);template<classX>voidg(constXx);template<classZ>voidh(Zz,Z*zp);// two different functions with the same type, but // within the function, t has different cv qualificationsf<int>(1);// function type is void(int), t is intf<constint>(1);// function type is void(int), t is const int// two different functions with the same type and the same x// (pointers to these two functions are not equal,// and function-local statics would have different addresses)g<int>(1);// function type is void(int), x is const intg<constint>(1);// function type is void(int), x is const int// only top-level cv-qualifiers are dropped:h<constint>(1,NULL);// function type is void(int, const int*) // z is const int, zp is const int*Function template overloading

Function templates and non-template functions may be overloaded.

A non-template function is always distinct from a template specialization with the same type. Specializations of different function templates are always distinct from each other even if they have the same type. Two function templates with the same return type and the same parameter list are distinct and can be distinguished by their explicit template argument list.

When an expression that uses type or non-type template parameters appears in the function parameter list or in the return type, that expression remains a part of the function template signature for the purpose of overloading:

template<intI,intJ>A<I+J>f(A<I>,A<J>);// overload #1template<intK,intL>A<K+L>f(A<K>,A<L>);// same as #1template<intI,intJ>A<I-J>f(A<I>,A<J>);// overload #2Two expressions involving template parameters are called equivalent if two function definitions that contain these expressions would be the same under

ODR

, that is, the two expressions contain the same sequence of tokens whose names are resolved to same entities via name lookup, except template parameters may be differently named. Two

lambda expressions

are never equivalent.(since C++20)

template<intI,intJ>voidf(A<I+J>);// template overload #1template<intK,intL>voidf(A<K+L>);// equivalent to #1When determining if two

dependent expressions

are equivalent, only the dependent names involved are considered, not the results of name lookup. If multiple declarations of the same template differ in the result of name lookup, the first such declaration is used:

template<classT>decltype(g(T()))h();// decltype(g(T())) is a dependent typeintg(int);template<classT>decltype(g(T()))h(){// redeclaration of h() uses earlier lookupreturng(T());// although the lookup here does find g(int)}inti=h<int>();// template argument substitution fails; g(int)// was not in scope at the first declaration of h()Two function templates are considered equivalent if

they are declared in the same scope

they have the same name

they have equivalent template parameter lists, meaning the lists are of the same length, and for each corresponding parameter pair, all of the following is true:

the two parameters are of the same kind (both types, both non-types, or both templates)

they are either both parameter packs or neither

(since C++11)if non-type, their types are equivalent,

if template, their template parameters are equivalent,

if one is declared with concept-name, they both are, and the concept-names are equivalent.

(since C++20)the expressions involving template parameters in their return types and parameter lists are equivalent

the expressions in their requires-clauses that follow the template parameter lists, if present, are equivalent

the expressions in their requires-clauses that follow the function declarators, if present, are equivalent

(since C++20)Two

potentially-evaluated

(since C++20) expressions involving template parameters are called functionally equivalent if they are not equivalent, but for any given set of template arguments, the evaluation of the two expressions results in the same value.

Two function templates are considered functionally equivalent if they are equivalent, except that one or more expressions that involve template parameters in their return types and parameter lists are functionally equivalent.

In addition, two function templates are functionally equivalent but not equivalent if their constraints are specified differently, but they accept and are satisfied by the same set of template argument lists.

(since C++20)If a program contains declarations of function templates that are functionally equivalent but not equivalent, the program is ill-formed; no diagnostic is required.

// equivalenttemplate<intI>voidf(A<I>,A<I+10>);// overload #1template<intI>voidf(A<I>,A<I+10>);// redeclaration of overload #1// not equivalenttemplate<intI>voidf(A<I>,A<I+10>);// overload #1template<intI>voidf(A<I>,A<I+11>);// overload #2// functionally-equivalent but not equivalent// This program is ill-formed, no diagnostic requiredtemplate<intI>voidf(A<I>,A<I+10>);// overload #1template<intI>voidf(A<I>,A<I+1+2+3+4>);// functionally equivalentWhen the same function template specialization matches more than one overloaded function template (this often results from

template argument deduction

), partial ordering of overloaded function templates is performed to select the best match.

Specifically, partial ordering takes place in the following situations:

1)

overload resolution

for a call to a function template specialization:

template<classX>voidf(Xa);template<classX>voidf(X*a);int*p;f(p);3) when a

placement operator delete

that is a function template specialization is selected to match a placement operator new:

4) when a

friend function declaration

, an

explicit instantiation

, or an

explicit specialization

refers to a function template specialization:

template<classX>voidf(Xa);// first template ftemplate<classX>voidf(X*a);// second template ftemplate<>voidf<>(int*a){}// explicit specialization// template argument deduction comes up with two candidates:// f<int*>(int*) and f<int>(int*)// partial ordering selects f<int>(int*) as more specializedInformally "A is more specialized than B" means "A accepts fewer types than B".

Formally, to determine which of any two function templates is more specialized, the partial ordering process first transforms one of the two templates as follows:

For each type, non-type, and template parameter, including parameter packs,(since C++11) a unique fictitious type, value, or template is generated and substituted into function type of the template

If only one of the two function templates being compared is a member function, and that function template is a non-static member of some class A, a new first parameter is inserted into its parameter list. Given cv as the cv-qualifiers of the function template and ref as the ref-qualifier of the function template(since C++11), the new parameter type is cvA& unless ref is &&, or ref is not present and the first parameter of the other template has rvalue reference type, in this case the type is cvA&&(since C++11). This helps the ordering of operators, which are looked up both as member and as non-member functions:

structA{};template<classT>structB{template<classR>intoperator*(R&);// #1};template<classT,classR>intoperator*(T&,R&);// #2intmain(){Aa;B<A>b;b*a;// template argument deduction for int B<A>::operator*(R&) gives R=A // for int operator*(T&, R&), T=B<A>, R=A// For the purpose of partial ordering, the member template B<A>::operator*// is transformed into template<class R> int operator*(B<A>&, R&);// partial ordering between // int operator*( T&, R&) T=B<A>, R=A// and int operator*(B<A>&, R&) R=A // selects int operator*(B<A>&, A&) as more specialized}After one of the two templates was transformed as described above,

template argument deduction

is executed using the transformed template as the argument template and the original template type of the other template as the parameter template. The process is then repeated using the second template (after transformations) as the argument and the first template in its original form as the parameter.

The types used to determine the order depend on the context:

in the context of a function call, the types are those function parameter types for which the function call has arguments (default function arguments, parameter packs,(since C++11) and ellipsis parameters are not considered -- see examples below)

in the context of a call to a user-defined conversion function, the return types of the conversion function templates are used

in other contexts, the function template type is used

Each type from the list above from the parameter template is deduced. Before deduction begins, each parameter P of the parameter template and the corresponding argument A of the argument template is adjusted as follows:

If both P and A are reference types before, determine which is more cv-qualified (in all other cases, cv-qualifications are ignored for partial ordering purposes)

If P is a reference type, it is replaced by the type referred to

If A is a reference type, it is replaced by the type referred to

If P is cv-qualified, P is replaced with cv-unqualified version of itself

If A is cv-qualified, A is replaced with cv-unqualified version of itself

After these adjustments, deduction of P from A is done following

template argument deduction from a type

.

If P is a function parameter pack, the type A of each remaining parameter type of the argument template is compared with the type P of the declarator-id of the function parameter pack. Each comparison deduces template arguments for subsequent positions in the template parameter packs expanded by the function parameter pack.

If A was transformed from a function parameter pack, it is compared with each remaining parameter type of the parameter template.

(since C++11)If the argument A of the transformed template-1 can be used to deduce the corresponding parameter P of template-2, but not vice versa, then this A is more specialized than P with regards to the type(s) that are deduced by this P/A pair.

If deduction succeeds in both directions, and the original P and A were reference types, then additional tests are made:

If A was lvalue reference and P was rvalue reference, A is considered to be more specialized than P

If A was more cv-qualified than P, A is considered to be more specialized than P

In all other cases, neither template is more specialized than the other with regards to the type(s) deduced by this P/A pair.

After considering every P and A in both directions, if, for each type that was considered,

template-1 is at least as specialized as template-2 for all types

template-1 is more specialized than template-2 for some types

template-2 is not more specialized than template-1 for any types OR is not at least as specialized for any types

Then template-1 is more specialized than template-2. If the conditions above are true after switching template order, then template-2 is more specialized than template-1. Otherwise, neither template is more specialized than the other.

In case of a tie, if one function template has a trailing parameter pack and the other does not, the one with the omitted parameter is considered to be more specialized than the one with the empty parameter pack.

(since C++11)If, after considering all pairs of overloaded templates, there is one that is unambiguously more specialized than all others, that template's specialization is selected, otherwise compilation fails.

In the following examples, the fictitious arguments will be called U1, U2:

template<classT>voidf(T);// template #1template<classT>voidf(T*);// template #2template<classT>voidf(constT*);// template #3voidm(){constint*p;f(p);// overload resolution picks: #1: void f(T ) [T = const int *]// #2: void f(T*) [T = const int]// #3: void f(const T *) [T = int]// partial ordering:// #1 from transformed #2: void(T) from void(U1*): P=T A=U1*: deduction ok: T=U1*// #2 from transformed #1: void(T*) from void(U1): P=T* A=U1: deduction fails// #2 is more specialized than #1 with regards to T// #1 from transformed #3: void(T) from void(const U1*): P=T, A=const U1*: ok// #3 from transformed #1: void(const T*) from void(U1): P=const T*, A=U1: fails// #3 is more specialized than #1 with regards to T// #2 from transformed #3: void(T*) from void(const U1*): P=T* A=const U1*: ok// #3 from transformed #2: void(const T*) from void(U1*): P=const T* A=U1*: fails// #3 is more specialized than #2 with regards to T// result: #3 is selected// in other words, f(const T*) is more specialized than f(T) or f(T*)}template<classT>voidf(T,T*);// #1template<classT>voidf(T,int*);// #2voidm(int*p){f(0,p);// deduction for #1: void f(T, T*) [T = int]// deduction for #2: void f(T, int*) [T = int]// partial ordering:// #1 from #2: void(T,T*) from void(U1,int*): P1=T, A1=U1: T=U1// P2=T*, A2=int*: T=int: fails// #2 from #1: void(T,int*) from void(U1,U2*): P1=T A1=U1: T=U1// P2=int* A2=U2*: fails// neither is more specialized w.r.t T, the call is ambiguous}template<classT>voidg(T);// template #1template<classT>voidg(T&);// template #2voidm(){floatx;g(x);// deduction from #1: void g(T ) [T = float]// deduction from #2: void g(T&) [T = float]// partial ordering:// #1 from #2: void(T) from void(U1&): P=T, A=U1 (after adjustment), ok// #2 from #1: void(T&) from void(U1): P=T (after adjustment), A=U1: ok// neither is more specialized w.r.t T, the call is ambiguous}template<classT>structA{A();};template<classT>voidh(constT&);// #1template<classT>voidh(A<T>&);// #2voidm(){A<int>z;h(z);// deduction from #1: void h(const T &) [T = A<int>]// deduction from #2: void h(A<T> &) [T = int]// partial ordering:// #1 from #2: void(const T&) from void(A<U1>&): P=T A=A<U1>: ok T=A<U1>// #2 from #1: void(A<T>&) from void(const U1&): P=A<T> A=const U1: fails// #2 is more specialized than #1 w.r.t TconstA<int>z2;h(z2);// deduction from #1: void h(const T&) [T = A<int>]// deduction from #2: void h(A<T>&) [T = int], but substitution fails// only one overload to choose from, partial ordering not tried, #1 is called}Since a call context considers only parameters for which there are explicit call arguments, those function parameter packs,(since C++11) ellipsis parameters, and parameters with default arguments, for which there is no explicit call argument, are ignored:

template<classT>voidf(T);// #1template<classT>voidf(T*,int=1);// #2voidm(int*ip){int*ip;f(ip);// calls #2 (T* is more specialized than T)}template<classT>voidg(T);// #1template<classT>voidg(T*,...);// #2voidm(int*ip){g(ip);// calls #2 (T* is more specialized than T)}template<classT,classU>structA{};template<classT,classU>voidf(U,A<U,T>*p=0);// #1template<classU>voidf(U,A<U,U>*p=0);// #2voidh(){f<int>(42,(A<int,int>*)0);// calls #2f<int>(42);// error: ambiguous}template<classT>voidg(T,T=T());// #1template<classT,class...U>voidg(T,U...);// #2voidh(){g(42);// error: ambiguous}template<classT,class...U>voidf(T,U...);// #1template<classT>voidf(T);// #2voidh(inti){f(&i);// calls #2 due to the tie-breaker between parameter pack and no parameter// (note: was ambiguous between DR692 and DR1395)}template<classT,class...U>voidg(T*,U...);// #1template<classT>voidg(T);// #2voidh(inti){g(&i);// OK: calls #1 (T* is more specialized than T)}template<class...T>intf(T*...);// #1template<classT>intf(constT&);// #2f((int*)0);// OK: selects #2; non-variadic template is more specialized than// variadic template (was ambiguous before DR1395 because deduction// failed in both directions)template<class...Args>voidf(Args...args);// #1template<classT1,class...Args>voidf(T1a1,Args...args);// #2template<classT1,classT2>voidf(T1a1,T2a2);// #3f();// calls #1f(1,2,3);// calls #2f(1,2);// calls #3; non-variadic template #3 is more// specialized than the variadic templates #1 and #2During template argument deduction within the partial ordering process, template parameters don't require to be matched with arguments, if the argument is not used in any of the types considered for partial ordering

template<classT>Tf(int);// #1template<classT,classU>Tf(U);// #2voidg(){f<int>(1);// specialization of #1 is explicit: T f(int) [T = int]// specialization of #2 is deduced: T f(U) [T = int, U = int]// partial ordering (only considering the argument type):// #1 from #2: T(int) from U1(U2): fails// #2 from #1: T(U) from U1(int): ok: U=int, T unused// calls #1}Partial ordering of function templates containing template parameter packs is independent of the number of deduced arguments for those template parameter packs.

template<class...>structTuple{};template<class...Types>voidg(Tuple<Types...>);// #1template<classT1,class...Types>voidg(Tuple<T1,Types...>);// #2template<classT1,class...Types>voidg(Tuple<T1,Types&...>);// #3g(Tuple<>());// calls #1g(Tuple<int,float>());// calls #2g(Tuple<int,float&>());// calls #3g(Tuple<int>());// calls #3(since C++11)To compile a call to a function template, the compiler has to decide between non-template overloads, template overloads, and the specializations of the template overloads.

template<classT>voidf(T);// #1: template overloadtemplate<classT>voidf(T*);// #2: template overloadvoidf(double);// #3: non-template overloadtemplate<>voidf(int);// #4: specialization of #1f('a');// calls #1f(newint(1));// calls #2f(1.0);// calls #3f(1);// calls #4Function overloads vs function specializations

Note that only non-template and primary template overloads participate in overload resolution. The specializations are not overloads and are not considered. Only after the overload resolution selects the best-matching primary function template, its specializations are examined to see if one is a better match.

template<classT>voidf(T);// #1: overload for all typestemplate<>voidf(int*);// #2: specialization of #1 for pointers to inttemplate<classT>voidf(T*);// #3: overload for all pointer typesf(newint(1));// calls #3, even though specialization of #1 would be a perfect matchIt is important to remember this rule while ordering the header files of a translation unit. For more examples of the interplay between function overloads and function specializations, expand below:

ExamplesConsider first some scenarios where the argument-dependent lookup is not employed. For that, we use the call (f)(t). As described in

ADL

, wrapping the function name in parentheses is suppressing the argument-dependent lookup.

Multiple overloads of f() declared before the point-of-reference (POR) in g().

Run this code

#include<iostream>structA{};template<classT>voidf(T){std::cout<<"#1\n";}// overload #1 before f() PORtemplate<classT>voidf(T*){std::cout<<"#2\n";}// overload #2 before f() PORtemplate<classT>voidg(T*t){(f)(t);// f() POR}intmain(){A*p=nullptr;g(p);// POR of g() and f()}// Both #1 and #2 are added to the candidate list;// #2 is selected because it is a better match.Output:

#2 A better matching template overload is declared after POR.

Run this code

#include<iostream>structA{};template<classT>voidf(T){std::cout<<"#1\n";}// #1template<classT>voidg(T*t){(f)(t);// f() POR}template<classT>voidf(T*){std::cout<<"#2\n";}// #2intmain(){A*p=nullptr;g(p);// POR of g() and f()}// Only #1 is added to the candidate list; #2 is defined after POR;// therefore, it is not considered for overloading even if it is a better match.Output:

#1 A better matching explicit template specialization is declared after POR.

Run this code

#include<iostream>structA{};template<classT>voidf(T){std::cout<<"#1\n";}// #1template<classT>voidg(T*t){(f)(t);// f() POR}template<>voidf<>(A*){std::cout<<"#3\n";}// #3intmain(){A*p=nullptr;g(p);// POR of g() and f()}// #1 is added to the candidate list; #3 is a better match defined after POR. The// candidate list consists of #1 which is eventually selected. After that, the explicit // specialization #3 of #1 declared after POI is selected because it is a better match. // This behavior is governed by 14.7.3/6 [temp.expl.spec] and has nothing to do with ADL.Output:

#3 A better matching template overload is declared after POR. The best matching explicit template specialization is declared after the better matching overload.

Run this code

#include<iostream>structA{};template<classT>voidf(T){std::cout<<"#1\n";}// #1template<classT>voidg(T*t){(f)(t);// f() POR}template<classT>voidf(T*){std::cout<<"#2\n";}// #2template<>voidf<>(A*){std::cout<<"#3\n";}// #3intmain(){A*p=nullptr;g(p);// POR of g() and f()}// #1 is the only member of the candidate list and it is eventually selected. // After that, the explicit specialization #3 is skipped because it actually // specializes #2 declared after POR.Output:

#1
Let's consider now those cases employing argument-dependent lookup (i.e., we use the more common call format f(t)).

A better matching template overload is declared after POR.

Run this code

#include<iostream>structA{};template<classT>voidf(T){std::cout<<"#1\n";}// #1template<classT>voidg(T*t){f(t);// f() POR}template<classT>voidf(T*){std::cout<<"#2\n";}// #2intmain(){A*p=nullptr;g(p);// POR of g() and f()}// #1 is added to the candidate list as a result of the ordinary lookup;// #2 is defined after POR but it is added to the candidate list via ADL lookup.// #2 is selected being the better match.Output:

#2 A better matching template overload is declared after POR. The best matching explicit template specialization is declared before the better matching overload.

Run this code

#include<iostream>structA{};template<classT>voidf(T){std::cout<<"#1\n";}// #1template<classT>voidg(T*t){f(t);// f() POR}template<>voidf<>(A*){std::cout<<"#3\n";}// #3template<classT>voidf(T*){std::cout<<"#2\n";}// #2intmain(){A*p=nullptr;g(p);// POR of g() and f()}// #1 is added to the candidate list as a result of the ordinary lookup;// #2 is defined after POR but it is added to the candidate list via ADL lookup.// #2 is selected among the primary templates, being the better match.// Since #3 is declared before #2, it is an explicit specialization of #1.// Hence the final selection is #2.Output:

#2 A better matching template overload is declared after POR. The best matching explicit template specialization is declared last.

Run this code

#include<iostream>structA{};template<classT>voidf(T){std::cout<<"#1\n";}// #1template<classT>voidg(T*t){f(t);// f() POR}template<classT>voidf(T*){std::cout<<"#2\n";}// #2template<>voidf<>(A*){std::cout<<"#3\n";}// #3intmain(){A*p=nullptr;g(p);// POR of g() and f()}// #1 is added to the candidate list as a result of the ordinary lookup;// #2 is defined after POR but it is added to the candidate list via ADL lookup.// #2 is selected among the primary templates, being the better match.// Since #3 is declared after #2, it is an explicit specialization of #2;// therefore, selected as the function to call.Output:

#3
Whenever the arguments are some C++ basic types, there are no ADL-associated namespaces. Hence, those scenarios are identical with the non-ADL examples above.

For detailed rules on overload resolution, see

overload resolution

.

Function template specialization

Keywords

template

,

extern

(since C++11)

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

CWG 214

C++98 the exact procedure of partial ordering was not specified specification added

CWG 532

C++98 the order between a non-static member function template
and a non-member function template was not specified specification added

CWG 581

C++98 template argument list in an explicit specialization or
instantiation of a constructor template was allowed forbidden

CWG 1321

C++98 it was unclear whether same dependent names in the
first declaration and a redeclaration are equivalent they are equivalent and
the meaning is same as
in the first declaration

CWG 1395

C++11 deduction failed when A was from a pack,
and there was no empty pack tie-breaker deduction allowed,
tie-breaker added

CWG 1406

C++11 the type of the new first parameter added for
a non-static member function template was
not relevant to the ref-qualifier of that template the type is an rvalue
reference type if the
ref-qualifier is &&

CWG 1446

C++11 the type of the new first parameter added for a non-static member
function template without ref-qualifier was an lvalue reference
type, even if that member function template is compared with a
function template whose first parameter has rvalue reference type the type is an
rvalue reference
type in this case

CWG 2373

C++98 new first parameters were added to the parameter lists
of static member function templates in partial ordering not added See also

class template

function declaration