Function template

From cppreference.com
< cpp‎ | language

A function template defines a family of functions.

Contents

[edit] Syntax

template < parameter-list > declaration

[edit] Explanation

declaration defines or declares a class (including struct and union), a member class or member enumeration type, a function or member function, a static data member at namespace scope, a variable or static data member at class scope, (since C++14) or a alias template (since C++11). It may also define a template specialization. This page focuses on function templates.

parameter-list is 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. For function templates, template parameters are declared in the same manner as for class templates: see class template page for details.

[edit] Function template instantiation

A function template by itself is not a type, or a function, or any other entity. 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).

[edit] Explicit instantiation

template return-type name < argument-list > ( parameter-list ) ; (1)
template return-type name ( parameter-list ) ; (2)
extern template return-type name < argument-list > ( parameter-list ) ; (3) (since C++11)
extern template return-type name ( 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.

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.


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<typename T>
void f(T s)
{
    std::cout << s << '\n';
}
 
template void f<double>(double); // instantiates f<double>(double)
template void f<>(char); // instantiates f<char>(char), template argument deduced
template void f(int); // instantiates f<int>(int), template argument deduced

Explicit 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 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 TU)

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<class T> T g(T x = &p) { return x; }
template int g<int>(int);   // OK even though &p isn’t an int.

[edit] Implicit instantiation

When code refers to a function in context that requires the function definition to exist, 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

#include <iostream>
 
template<typename T>
void f(T s)
{
    std::cout << s << '\n';
}
 
int main()
{
    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 (*ptr)(std::string) = f; // instantiates f<string>(string)
}


Note: omitting <> entirely allows overload resolution to examine both template and non-template overloads.

[edit] 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<typename To, typename From> To convert(From f);
 
void g(double d) 
{
    int i = convert<int>(d); // calls convert<int,double>(double)
    char c = 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>
int main() 
{
    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.

[edit] Explicit template arguments

Template arguments of a function template may be obtained from

  • template argument deduction
  • default template arguments
  • specified explicitly in a function call expression
  • specified explicitly when an address of a function is taken
  • specified explicitly when a reference to function is initialized
  • specified explicitly when a pointer to member function is formed
  • specified in an explicit specialization
  • specified in an explicit instantiation
  • specified in a friend declaration

There is no way to explicitly specify template arguments to 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)

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> void f(Types ... values);
void g() {
  f<int*, float*>(0, 0, 0); // Types = {int*, float*, int}
}

[edit] Template argument substitution

When all template arguments have been specified, deduced or obtained from default template arguments, every uses 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 <class T> void f(T t);
template <class X> void g(const X x);
template <class Z> void h(Z z, Z* zp);
 
// two different functions with the same type, but 
// within the function, t has different cv qualifications
f<int>(1);       // function type is void(int), t is int
f<const int>(1); // function type is void(int), t is const const
 
// 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 int
g<const int>(1); // function type is void(int), x is const int
 
// only top-level cv-qualifiers are dropped:
h<const int>(1, NULL); // function type is void(int, const int*) 
                       // z is const int, zp is const int*

[edit] Function template overloading

More than one 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 with 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<int I, int J> A<I+J> f(A<I>, A<J>); // overload #1
template<int K, int L> A<K+L> f(A<K>, A<L>); // same as #1
template<int I, int J> A<I-J> f(A<I>, A<J>); // overload #2

Two expressions involving template parameters are called equivalent if two function definitions that contain these expressions would be the same under ODR rules, 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.

template <int I, int J> void f(A<I+J>);  // template overload #1
template <int K, int L> void f(A<K+L>);  // equivalent to #1

When 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 <class T> decltype(g(T())) h(); // decltype(g(T())) is a dependent type
int g(int);
template <class T> decltype(g(T())) h() { // redeclaration of h() uses earlier lookup
    return g(T());                     // ...although the lookup here does find g(int)
}
int i = h<int>();   // template argument substitution fails; g(int)
                    // was not in scope at the first declaration of h()
(since C++14)

Two function templates are considered equivalent if

  • they are declared in the same scope
  • they have the same name
  • they have identical template parameter lists
  • the expressions involving template parameters in their return types and parameter lists are equivalent

Two 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 the

  • they are equivalent (as above) except that one or more expressions that involve template parameters in their return types and parameter lists are functionally equivalent

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

// equivalent
template <int I> void f(A<I>, A<I+10>); // overload #1
template <int I> void f(A<I>, A<I+10>); // redeclaration of overload #1
 
// not equivalent
template <int I> void f(A<I>, A<I+10>); // overload #1
template <int I> void f(A<I>, A<I+11>); // overload #2
 
// functionally-equivalent but not equivalent
// This program is ill-formed, no diagnostic required
template <int I> void f(A<I>, A<I+10>); // overload #1
template <int I> void f(A<I>, A<I+1+2+3+4>); // functionally equivalent

When 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
2) when the address of a function template specialization is taken
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<class X> void f(X a);  // first template f
template<class X> void f(X* a); // second template f
template<> void f<>(int *a) {} // explicit specialization
 // template argument deduction comes up with two candidates:
 // foo<int*>(int*) and f<int>(int*)
 // partial ordering selects f<int>(int*) as more specialized

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, 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 non-static member of some class A, a new first parameter is inserted into its parameter list, whose type is cv A&& if the member function template is &&-qualified and cv A& otherwise (cv is the cv-qualification of the member function template) -- this helps the ordering of operators, which are looked up both as member and as non-member functions:
struct A {};
template<class T> struct B {
  template<class R> int operator*(R&);                     // #1
};
template<class T, class R> int operator*(T&, R&);          // #2
int main() {
  A a;
  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, 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-qualificiations 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
  • If A was transformed from a function parameter pack and P is not a parameter pack, deduction fails

After these adjustments, deduction of P from A is done following template argument deduction from a type.

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, than template-2 is more specialized than template-1. Otherwise, neither template is more specialized than the other.

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<class T> void f(T);        // template #1
template<class T> void f(T*);       // template #2
template<class T> void f(const T*); // template #3
void m() {
  const int* 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<class T> void f(T, T*);    // #1
template<class T> void f(T, int*);  // #2
void m(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<class T> void g(T);  // template #1
template<class T> void g(T&); // template #2
void m() {
  float x;
  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<class T> struct A { A(); };
 
template<class T> void h(const T&); // #1
template<class T> void h(A<T>&);    // #2
void m() {
  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 T
 
  const A<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 in a call context considers only parameters for which there are explicit call arguments, those function parameter packs, ellipsis parameters, and parameters with default arguments, for which there is no explicit call argument, are ignored:

template<class T>  void  f(T);               // #1
template<class T>  void  f(T*, int=1);       // #2
void m(int* ip) {
  int* ip;
  f(ip);     // calls #2 (T* is more specialized than T)
}
template<class  T>  void  g(T);               // #1
template<class  T>  void  g(T*, ...);         // #2
void m(int* ip) {
   g(ip);     // calls #2 (T* is more specialized than T)
}
template<class T, class U> struct A { };
template<class T, class U> void f(U, A<U,T>* p = 0); // #1
template<         class U> void f(U, A<U,U>* p = 0); // #2
void h() {
  f<int>(42, (A<int, int>*)0);  // calls #2
  f<int>(42);                   // error: ambiguous
}
template<class T           >  void g(T, T = T());  // #1
template<class T, class... U> void g(T, U ...);    // #2
void h() {
  g(42);  // error: ambiguous
}
template<class  T, class... U> void f(T, U...);           // #1
template<class  T            > void f(T);                 // #2
void h(int i) {
  f(&i);        // error: ambiguous
}
template<class  T, class... U> void g(T*, U...);          // #1
template<class  T            > void g(T);                 // #2
void h(int i) {
  g(&i);        // OK: calls #1 (T* is more specialized than T)
}
template<class... Args>           void f(Args... args);               // #1
template<class T1, class... Args> void f(T1 a1, Args... args);        // #2
template<class T1, class T2>      void f(T1 a1, T2 a2);               // #3
f();                  // calls #1
f(1, 2, 3);           // calls #2
f(1, 2);              // calls #3; non-variadic template #3 is more
                      // specialized than the variadic templates #1 and #2

During 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 <class T>          T f(int);  // #1
template <class T, class U> T f(U);    // #2
void g() {
  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...> struct Tuple { };
template<          class... Types> void g(Tuple<Types ...>);        // #1
template<class T1, class... Types> void g(Tuple<T1, Types ...>);    // #2
template<class T1, class... Types> void g(Tuple<T1, Types& ...>);   // #3
 
g(Tuple<>());                     // calls #1
g(Tuple<int, float>());           // calls #2
g(Tuple<int, float&>());          // calls #3
g(Tuple<int>());                  // calls #3


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< class T > void f(T);              // #1: template overload
template< class T > void f(T*);             // #2: template overload
void                     f(double);         // #3: nontemplate overload
template<>          void f(int);            // #4: specialization of #1
 
f('a');        // calls #1
f(new int(1)); // calls #2
f(1.0);        // calls #3
f(1);          // calls #4

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< class T > void f(T);    // #1: overload for all types
template<>          void f(int*); // #2: specialization of #1 for pointers to int
template< class T > void f(T*);   // #3: overload for all pointer types
 
f(new int(1)); // calls #3, even though specialization of #1 would be a perfect match

For detailed rules on overload resolution, see overload resolution

[edit] Function template specialization

[edit] See Also