104 lines
5.0 KiB
Markdown
104 lines
5.0 KiB
Markdown
[temp.mem]
|
||
|
||
# 13 Templates [[temp]](./#temp)
|
||
|
||
## 13.7 Template declarations [[temp.decls]](temp.decls#temp.mem)
|
||
|
||
### 13.7.3 Member templates [temp.mem]
|
||
|
||
[1](#1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L2951)
|
||
|
||
A template can be declared within a class or class template; such a template
|
||
is called a member template[.](#1.sentence-1)
|
||
|
||
A member template can be defined within or outside its class definition or
|
||
class template definition[.](#1.sentence-2)
|
||
|
||
A member template of a class template that is defined outside of its class
|
||
template definition shall be specified with
|
||
a [*template-head*](temp.pre#nt:template-head "13.1 Preamble [temp.pre]") equivalent to that
|
||
of the class template followed by
|
||
a [*template-head*](temp.pre#nt:template-head "13.1 Preamble [temp.pre]") equivalent to that
|
||
of the member template ([[temp.over.link]](temp.over.link "13.7.7.2 Function template overloading"))[.](#1.sentence-3)
|
||
|
||
[*Example [1](#example-1)*: template<class T> struct string {template<class T2> int compare(const T2&); template<class T2> string(const string<T2>& s) { /* ... */ }};
|
||
|
||
template<class T> template<class T2> int string<T>::compare(const T2& s) {} â *end example*]
|
||
|
||
[*Example [2](#example-2)*: template<typename T> concept C1 = true;template<typename T> concept C2 = sizeof(T) <= 4;
|
||
|
||
template<C1 T> struct S {template<C2 U> void f(U); template<C2 U> void g(U);};
|
||
|
||
template<C1 T> template<C2 U>void S<T>::f(U) { } // OKtemplate<C1 T> template<typename U>void S<T>::g(U) { } // error: no matching function in S<T> â *end example*]
|
||
|
||
[2](#2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L2990)
|
||
|
||
A local class of non-closure type shall not have member templates[.](#2.sentence-1)
|
||
|
||
[Access control rules](class.access "11.8 Member access control [class.access]") apply to member template names[.](#2.sentence-2)
|
||
|
||
A destructor shall not be a member
|
||
template[.](#2.sentence-3)
|
||
|
||
A non-template member function ([[dcl.fct]](dcl.fct "9.3.4.6 Functions")) 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[.](#2.sentence-4)
|
||
|
||
When both exist, a use of that name and type refers to the
|
||
non-template member unless an explicit template argument list is supplied[.](#2.sentence-5)
|
||
|
||
[*Example [3](#example-3)*: template <class T> struct A {void f(int); template <class T2> void f(T2);};
|
||
|
||
template <> void A<int>::f(int) { } // non-template member functiontemplate <> template <> void A<int>::f<>(int) { } // member function template specializationint main() { A<char> ac;
|
||
ac.f(1); // non-template ac.f('c'); // template ac.f<>(1); // template} â *end example*]
|
||
|
||
[3](#3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L3021)
|
||
|
||
A member function template shall not be declared virtual[.](#3.sentence-1)
|
||
|
||
[*Example [4](#example-4)*: template <class T> struct AA {template <class C> virtual void g(C); // errorvirtual void f(); // OK}; â *end example*]
|
||
|
||
[4](#4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L3032)
|
||
|
||
A specialization of
|
||
a member function template does not override a virtual function from a
|
||
base class[.](#4.sentence-1)
|
||
|
||
[*Example [5](#example-5)*: class B {virtual void f(int);};
|
||
|
||
class D : public B {template <class T> 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](#5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L3049)
|
||
|
||
[*Note [1](#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]](class.conv.fct "11.4.8.3 Conversion functions"))[.](#5.sentence-1)
|
||
|
||
[*Example [6](#example-6)*: struct A {template <class T> operator T*();};template <class T> 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*](expr.prim.splice#nt:splice-expression "7.5.9 Expression splicing [expr.prim.splice]")[.](#5.sentence-2)
|
||
|
||
There is no analogous syntax to form a [*template-id*](temp.names#nt:template-id "13.3 Names of template specializations [temp.names]") ([[temp.names]](temp.names "13.3 Names of template specializations"))
|
||
for such a function
|
||
by providing an explicit template argument list ([[temp.arg.explicit]](temp.arg.explicit "13.10.2 Explicit template argument specification"))[.](#5.sentence-3)
|
||
|
||
â *end note*]
|