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

8.7 KiB
Raw Permalink Blame History

[temp.arg.general]

13 Templates [temp]

13.4 Template arguments [temp.arg]

13.4.1 General [temp.arg.general]

1

#

The type and form of each template-argument specified in a template-id or in a splice-specialization-specifier shall match the type and form specified for the corresponding parameter declared by the template in itstemplate-parameter-list.

When the parameter declared by the template is atemplate parameter pack, it will correspond to zero or moretemplate-arguments.

[Example 1: template class Array { T* v; int sz;public:explicit Array(int); T& operator; T& elem(int i) { return v[i]; }};

Array v1(20);typedef std::complex dcomplex; // std::complex is a standard library template Array v2(30); Array v3(40);

void bar() { v1[3] = 7; v2[3] = v3.elem(4) = dcomplex(7,8);} — end example]

2

#

The template argument list of a template-head is a template argument list in which the nth template argument has the value of the nth template parameter of the template-head.

If the nth template parameter is a template parameter pack ([temp.variadic]), the nth template argument is a pack expansion whose pattern is the name of the template parameter pack.

3

#

In atemplate-argument, an ambiguity between atype-id and an expression is resolved to atype-id, regardless of the form of the correspondingtemplate-parameter.109

[Example 2: template void f();template void f();

void g() { f<int()>(); // int() is a type-id: call the first f()} — end example]

4

#

[Note 1:

Names used in a template-argument are subject to access control where they appear.

Because a template parameter is not a class member, no access control applies where the template parameter is used.

— end note]

[Example 3: template class X {static T t;};

class Y {private:struct S { /* ... */ }; X x; // OK, S is accessible// X<Y::S> has a static member of type Y::S// OK, even though Y::S is private};

X<Y::S> y; // error: S not accessible — end example]

For a template argument that is a class type or a class template, the template definition has no special access rights to the members of the template argument.

[Example 4: template <template class T> class A {typename T::S s;};

template class B {private:struct S { /* ... */ };};

A b; // error: A has no access to B::S — end example]

5

#

When template argument packs or default template arguments are used, a template-argument list can be empty.

In that case the empty<> brackets shall still be used as thetemplate-argument-list.

[Example 5: template class String; String<>* p; // OK, String String* q; // syntax errortemplate<class ... Elements> class Tuple; Tuple<>* t; // OK, Elements is empty Tuple* u; // syntax error — end example]

6

#

An explicit destructor call ([class.dtor]) for an object that has a type that is a class template specialization may explicitly specify thetemplate-arguments.

[Example 6: template struct A {~A();};void f(A* p, A* q) { p->A::~A(); // OK, destructor call q->A::~A(); // OK, destructor call} — end example]

7

#

If the use of a template argument gives rise to an ill-formed construct in the instantiation of a template specialization, the program is ill-formed.

8

#

When name lookup for the component name of atemplate-id finds an overload set, both non-template functions in the overload set and function templates in the overload set for which thetemplate-arguments do not match thetemplate-parameters are ignored.

[Note 2:

If none of the function templates have matchingtemplate-parameters, the program is ill-formed.

— end note]

9

#

When a simple-template-id orsplice-specialization-specifier does not designate a function, a default template-argument isimplicitly instantiated when the value of that default argument is needed.

[Example 7: template<typename T, typename U = int> struct S { }; S* p; // the type of p is S<bool, int>*

The default argument for U is instantiated to form the type S<bool, int>*.

— end example]

10

#

A template-argument followed by an ellipsis is a pack expansion.

109)109)

There is no such ambiguity in a defaulttemplate-argument because the form of thetemplate-parameter determines the allowable forms of thetemplate-argument.