Files
2025-10-25 03:02:53 +03:00

26 KiB
Raw Permalink Blame History

[temp.fct]

13 Templates [temp]

13.7 Template declarations [temp.decls]

13.7.7 Function templates [temp.fct]

13.7.7.1 General [temp.fct.general]

1

#

A function template defines an unbounded set of related functions.

[Example 1:

A family of sort functions can be declared like this:template class Array { };template void sort(Array&);

— end example]

2

#

[Note 1:

A function template can have the same name as other function templates and non-template functions ([dcl.fct]) in the same scope.

— end note]

A non-template function is not related to a function template (i.e., it is never considered to be a specialization), even if it has the same name and type as a potentially generated function template specialization.115

115)115)

That is, declarations of non-template functions do not merely guide overload resolution of function template specializations with the same name.

If such a non-template function is odr-used ([basic.def.odr]) in a program, it must be defined; it will not be implicitly instantiated using the function template definition.

1

#

It is possible to overload function templates so that two different function template specializations have the same type.

[Example 1:

// translation unit 1:templatevoid f(T*);void g(int* p) { f(p); // calls f(int*)}

// translation unit 2:templatevoid f(T);void h(int* p) { f(p); // calls f<int*>(int*)}

— end example]

2

#

Such specializations are distinct functions and do not violate theone-definition rule.

3

#

The signature of a function template is defined in [intro.defs].

The names of the template parameters are significant only for establishing the relationship between the template parameters and the rest of the signature.

[Note 1:

Two distinct function templates can have identical function return types and function parameter lists, even if overload resolution alone cannot distinguish them.

template void f();template void f(); // OK, overloads the first template// distinguishable with an explicit template argument list — end note]

4

#

When an expression that references a template parameter is used in the function parameter list or the return type in the declaration of a function template, the expression that references the template parameter is part of the signature of the function template.

This is necessary to permit a declaration of a function template in one translation unit to be linked with another declaration of the function template in another translation unit and, conversely, to ensure that function templates that are intended to be distinct are not linked with one another.

[Example 2: template <int I, int J> A<I+J> f(A, A); // #1template <int K, int L> A<K+L> f(A, A); // same as #1template <int I, int J> A f(A, A); // different from #1 — end example]

[Note 2:

Most expressions that use template parameters use constant template parameters, but it is possible for an expression to reference a type parameter.

For example, a template type parameter can be used in thesizeof operator.

— end note]

5

#

Two expressions involving template parameters are consideredequivalent if two function definitions containing the expressions would satisfy the one-definition rule, except that the tokens used to name the template parameters may differ as long as a token used to name a template parameter in one expression is replaced by another token that names the same template parameter in the other expression.

Two unevaluated operands that do not involve template parameters are considered equivalent if two function definitions containing the expressions would satisfy the one-definition rule, except that the tokens used to name types and declarations may differ as long as they name the same entities, and the tokens used to form concept-ids ([temp.names]) may differ as long as the two template-ids are the same ([temp.type]).

[Note 3:

For instance, A<42> and A<40+2> name the same type.

— end note]

Two lambda-expressions are never considered equivalent.

[Note 4:

The intent is to avoid lambda-expressions appearing in the signature of a function template with external linkage.

— end note]

For determining whether two dependent names ([temp.dep]) are equivalent, only the name itself is considered, not the result of name lookup.

[Note 5:

If such a dependent name is unqualified, it is looked up from a first declaration of the function template ([temp.res.general]).

— end note]

[Example 3: template <int I, int J> void f(A<I+J>); // #1template <int K, int L> void f(A<K+L>); // same as #1template decltype(g(T())) h();int g(int);template decltype(g(T())) h() // redeclaration of h() uses the earlier lookup…{ return g(T()); } // … although the lookup here does find g(int)int i = h(); // template argument substitution fails; g(int)// not considered at the first declaration of h()// ill-formed, no diagnostic required: the two expressions are functionally equivalent but not equivalenttemplate void foo(const char (*s)[([]{}, N)]);template void foo(const char (*s)[([]{}, N)]);

// two different declarations because the non-dependent portions are not considered equivalenttemplate void spam(decltype([]{}) (*s)[sizeof(T)]);template void spam(decltype([]{}) (*s)[sizeof(T)]); — end example]

Two potentially-evaluated expressions involving template parameters that are not equivalent arefunctionally equivalent if, for any given set of template arguments, the evaluation of the expression results in the same value.

Two unevaluated operands that are not equivalent are functionally equivalent if, for any given set of template arguments, the expressions perform the same operations in the same order with the same entities.

[Note 6:

For instance, one could have redundant parentheses.

— end note]

[Example 4: template concept C = true;template struct A {void f() requires C<42>; // #1void f() requires true; // OK, different functions}; — end example]

6

#

Two template-heads areequivalent if their template-parameter-lists have the same length, corresponding template-parameters are equivalent and are both declared with type-constraints that are equivalent if either template-parameter is declared with a type-constraint, and if either template-head has a requires-clause, they both haverequires-clauses and the correspondingconstraint-expressions are equivalent.

Two template-parameters areequivalent under the following conditions:

they declare template parameters of the same kind,

if either declares a template parameter pack, they both do,

if they declare constant template parameters, they have equivalent types ignoring the use of type-constraints for placeholder types, and

if they declare template template parameters, their template-heads are equivalent.

When determining whether types or type-constraints are equivalent, the rules above are used to compare expressions involving template parameters.

Two template-heads arefunctionally equivalent if they accept and are satisfied by ([temp.constr.constr]) the same set of template argument lists.

7

#

If the validity or meaning of the program depends on whether two constructs are equivalent, and they are functionally equivalent but not equivalent, the program is ill-formed, no diagnostic required.

Furthermore, if two declarations A and B of function templates

introduce the same name,

have corresponding signatures ([basic.scope.scope]),

would declare the same entity, when considering A and B to correspond in that determination ([basic.link]), and

accept and are satisfied by the same set of template argument lists,

but do not correspond, the program is ill-formed, no diagnostic required.

8

#

[Note 7:

This rule guarantees that equivalent declarations will be linked with one another, while not requiring implementations to use heroic efforts to guarantee that functionally equivalent declarations will be treated as distinct.

For example, the last two declarations are functionally equivalent and would cause a program to be ill-formed:// guaranteed to be the sametemplate void f(A, A<I+10>);template void f(A, A<I+10>);

// guaranteed to be differenttemplate void f(A, A<I+10>);template void f(A, A<I+11>);

// ill-formed, no diagnostic requiredtemplate void f(A, A<I+10>);template void f(A, A<I+1+2+3+4>);

— end note]

13.7.7.3 Partial ordering of function templates [temp.func.order]

1

#

If multiple function templates share a name, the use of that name can be ambiguous because template argument deduction ([temp.deduct]) may identify a specialization for more than one function template.

Partial ordering of overloaded function template declarations is used in the following contexts to select the function template to which a function template specialization refers:

during overload resolution for a call to a function template specialization ([over.match.best]);

when the address of a function template specialization is taken;

when a placement operator delete that is a function template specialization is selected to match a placement operator new ([basic.stc.dynamic.deallocation], [expr.new]);

when a friend function declaration, anexplicit instantiation or an explicit specialization refers to a function template specialization.

2

#

Partial ordering selects which of two function templates is more specialized than the other by transforming each template in turn (see next paragraph) and performing template argument deduction using the function type.

The deduction process determines whether one of the templates is more specialized than the other.

If so, the more specialized template is the one chosen by the partial ordering process.

If both deductions succeed, the partial ordering selects the more constrained template (if one exists) as determined below.

3

#

To produce the transformed template, for each type, constant, type template, variable template, or concept template parameter (including template parameter packs ([temp.variadic]) thereof) synthesize a unique type, value, class template, variable template, or concept, respectively, and substitute it for each occurrence of that parameter in the function type of the template.

[Note 1:

The type replacing the placeholder in the type of the value synthesized for a constant template parameter is also a unique synthesized type.

— end note]

4

#

A synthesized template has the same template-head as its corresponding template template parameter.

5

#

Each function template M that is a member function is considered to have a new first parameter of type X(M), described below, inserted in its function parameter list.

If exactly one of the function templates was considered by overload resolution via a rewritten candidate ([over.match.oper]) with a reversed order of parameters, then the order of the function parameters in its transformed template is reversed.

For a function template M with cv-qualifiers cv that is a member of a class A:

  • (5.1)

    The type X(M) is “rvalue reference to cv A” if the optional ref-qualifier ofM is && or if M has no ref-qualifier and the positionally-corresponding parameter of the other transformed template has rvalue reference type; if this determination depends recursively upon whether X(M) is an rvalue reference type, it is not considered to have rvalue reference type.

  • (5.2)

    Otherwise, X(M) is “lvalue reference to cv A”.

[Note 2:

This allows a non-static member to be ordered with respect to a non-member function and for the results to be equivalent to the ordering of two equivalent non-members.

— end note]

[Example 1: struct A { };template struct B {template int operator*(R&); // #1};

template<class T, class R> int operator*(T&, R&); // #2// The declaration of B::operator* is transformed into the equivalent of// template int operator*(B&, R&); // #1aint main() { A a; B b; b * a; // calls #1} — end example]

6

#

Using the transformed function template's function type, perform type deduction against the other template as described in [temp.deduct.partial].

[Example 2: template struct A { A(); };

template void f(T);template void f(T*);template void f(const T*);

template void g(T);template void g(T&);

template void h(const T&);template void h(A&);

void m() {const int* p; f(p); // f(const T*) is more specialized than f(T) or f(T*)float x; g(x); // ambiguous: g(T) or g(T&) A z; h(z); // overload resolution selects h(A&)const A z2; h(z2); // h(const T&) is called because h(A&) is not callable} — end example]

7

#

[Note 3:

Since, in a call context, such type deduction considers only parameters for which there are explicit call arguments, some parameters are ignored (namely, function parameter packs, parameters with default arguments, and ellipsis parameters).

[Example 3: template void f(T); // #1template void f(T*, int=1); // #2template void g(T); // #3template void g(T*, ...); // #4int main() {int* ip; f(ip); // calls #2 g(ip); // calls #4} — end example]

[Example 4: template<class T, class U> struct A { };

template<class T, class U> void f(U, A<U, T>* p = 0); // #1template< class U> void f(U, A<U, U>* p = 0); // #2template void g(T, T = T()); // #3template<class T, class... U> void g(T, U ...); // #4void h() { f(42, (A<int, int>*)0); // calls #2 f(42); // error: ambiguous g(42); // error: ambiguous} — end example]

[Example 5: template<class T, class... U> void f(T, U...); // #1template void f(T); // #2template<class T, class... U> void g(T*, U...); // #3template void g(T); // #4void h(int i) { f(&i); // OK, calls #2 g(&i); // OK, calls #3} — end example]

— end note]

8

#

If deduction against the other template succeeds for both transformed templates, constraints can be considered as follows:

If exactly one of the templates was considered by overload resolution via a rewritten candidate with reversed order of parameters: + (8.2.1.1) If, for either template, some of the template parameters are not deducible from their function parameters, neither template is more specialized than the other.

+
      [(8.2.1.2)](#temp.func.order-8.2.1.2)

If there is either no reordering or more than one reordering of the associated template-parameter-list such that - (8.2.1.2.1)

the corresponding template-parameters of the template-parameter-lists are equivalent and

  - [(8.2.1.2.2)](#temp.func.order-8.2.1.2.2)

the function parameters that positionally correspond between the two templates are of the same type,

neither template is more specialized than the other.

Otherwise, if the corresponding template-parameters of the template-parameter-lists are not equivalent ([temp.over.link]) or if the function parameters that positionally correspond between the two templates are not of the same type, neither template is more specialized than the other.

  • (8.3)

    Otherwise, if the context in which the partial ordering is done is that of a call to a conversion function and the return types of the templates are not the same, then neither template is more specialized than the other.

  • (8.4)

    Otherwise, if one template is more constrained than the other ([temp.constr.order]), the more constrained template is more specialized than the other.

  • (8.5)

    Otherwise, neither template is more specialized than the other.

[Example 6: template constexpr bool True = true;template concept C = True;

void f(C auto &, auto &) = delete;template void f(Q &, C auto &);

void g(struct A *ap, struct B *bp) { f(*ap, *bp); // OK, can use different methods to produce template parameters}template <typename T, typename U> struct X {};

template <typename T, C U, typename V> bool operator==(X<T, U>, V) = delete;template <C T, C U, C V> bool operator==(T, X<U, V>);

void h() { X<void *, int>{} == 0; // OK, correspondence of [T, U, V] and [U, V, T]} — end example]