50 lines
2.2 KiB
Markdown
50 lines
2.2 KiB
Markdown
[temp.mem.func]
|
||
|
||
# 13 Templates [[temp]](./#temp)
|
||
|
||
## 13.7 Template declarations [[temp.decls]](temp.decls#temp.mem.func)
|
||
|
||
### 13.7.2 Class templates [[temp.class]](temp.class#temp.mem.func)
|
||
|
||
#### 13.7.2.2 Member functions of class templates [temp.mem.func]
|
||
|
||
[1](#1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L2751)
|
||
|
||
A member function
|
||
of a class template
|
||
may be defined outside of the class
|
||
template definition in which it is declared[.](#1.sentence-1)
|
||
|
||
[*Example [1](#example-1)*: template<class T> class Array { T* v; int sz;public:explicit Array(int);
|
||
T& operator[](int);
|
||
T& elem(int i) { return v[i]; }};
|
||
|
||
declares three member functions of a class template[.](#1.sentence-2)
|
||
|
||
The subscript function can be defined like this:template<class T> T& Array<T>::operator[](int i) {if (i<0 || sz<=i) error("Array: range error"); return v[i];}
|
||
|
||
A constrained member function can be defined out of line:template<typename T> concept C = requires {typename T::type;};
|
||
|
||
template<typename T> struct S {void f() requires C<T>; void g() requires C<T>;};
|
||
|
||
template<typename T>void S<T>::f() requires C<T> { } // OKtemplate<typename T>void S<T>::g() { } // error: no matching function in S<T>
|
||
|
||
â *end example*]
|
||
|
||
[2](#2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L2796)
|
||
|
||
The[*template-argument*](temp.names#nt:template-argument "13.3 Names of template specializations [temp.names]")*s* for a member function of a class template are determined by the[*template-argument*](temp.names#nt:template-argument "13.3 Names of template specializations [temp.names]")*s* of the type of the object for which the member function is called[.](#2.sentence-1)
|
||
|
||
[*Example [2](#example-2)*:
|
||
|
||
The[*template-argument*](temp.names#nt:template-argument "13.3 Names of template specializations [temp.names]") forArray<T>::operator[] will be determined by theArray to which the subscripting operation is applied[.](#2.sentence-2)
|
||
|
||
Array<int> v1(20);
|
||
Array<dcomplex> v2(30);
|
||
|
||
v1[3] = 7; // Array<int>::operator[] v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[] â *end example*]
|