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

5.0 KiB
Raw Permalink Blame History

[temp.mem]

13 Templates [temp]

13.7 Template declarations [temp.decls]

13.7.3 Member templates [temp.mem]

1

#

A template can be declared within a class or class template; such a template is called a member template.

A member template can be defined within or outside its class definition or class template definition.

A member template of a class template that is defined outside of its class template definition shall be specified with a template-head equivalent to that of the class template followed by a template-head equivalent to that of the member template ([temp.over.link]).

[Example 1: template struct string {template int compare(const T2&); template string(const string& s) { /* ... */ }};

template template int string::compare(const T2& s) {} — end example]

[Example 2: template concept C1 = true;template concept C2 = sizeof(T) <= 4;

template struct S {template void f(U); template void g(U);};

template templatevoid S::f(U) { } // OKtemplate templatevoid S::g(U) { } // error: no matching function in S — end example]

2

#

A local class of non-closure type shall not have member templates.

Access control rules apply to member template names.

A destructor shall not be a member template.

A non-template member function ([dcl.fct]) with a given name and type and a member function template of the same name, which could be used to generate a specialization of the same type, can both be declared in a class.

When both exist, a use of that name and type refers to the non-template member unless an explicit template argument list is supplied.

[Example 3: template struct A {void f(int); template void f(T2);};

template <> void A::f(int) { } // non-template member functiontemplate <> template <> void A::f<>(int) { } // member function template specializationint main() { A ac; ac.f(1); // non-template ac.f('c'); // template ac.f<>(1); // template} — end example]

3

#

A member function template shall not be declared virtual.

[Example 4: template struct AA {template virtual void g(C); // errorvirtual void f(); // OK}; — end example]

4

#

A specialization of a member function template does not override a virtual function from a base class.

[Example 5: class B {virtual void f(int);};

class D : public B {template void f(T); // does not override B::f(int)void f(int i) { f<>(i); } // overriding function that calls the function template specialization}; — end example]

5

#

[Note 1:

A specialization of a conversion function template is named in the same way as a non-template conversion function that converts to the same type ([class.conv.fct]).

[Example 6: struct A {template operator T*();};template A::operator T*() { return 0; }template <> A::operator char*() { return 0; } // specializationtemplate A::operator void*(); // explicit instantiationint main() { A a; int* ip; ip = a.operator int*(); // explicit call to template operator A::operator int*()} — end example]

An expression designating a particular specialization of a conversion function template can only be formed with a splice-expression.

There is no analogous syntax to form a template-id ([temp.names]) for such a function by providing an explicit template argument list ([temp.arg.explicit]).

— end note]