tmp/tmpi2656oci/{from.md → to.md}
RENAMED
|
@@ -14,36 +14,54 @@ public:
|
|
| 14 |
T& operator[](int);
|
| 15 |
T& elem(int i) { return v[i]; }
|
| 16 |
};
|
| 17 |
```
|
| 18 |
|
| 19 |
-
declares three
|
| 20 |
-
defined like this:
|
| 21 |
|
| 22 |
``` cpp
|
| 23 |
template<class T> T& Array<T>::operator[](int i) {
|
| 24 |
if (i<0 || sz<=i) error("Array: range error");
|
| 25 |
return v[i];
|
| 26 |
}
|
| 27 |
```
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
— *end example*]
|
| 30 |
|
| 31 |
The *template-argument*s for a member function of a class template are
|
| 32 |
determined by the *template-argument*s of the type of the object for
|
| 33 |
which the member function is called.
|
| 34 |
|
| 35 |
[*Example 2*:
|
| 36 |
|
| 37 |
-
The *template-argument* for `Array<T>::operator[]
|
| 38 |
-
|
| 39 |
|
| 40 |
``` cpp
|
| 41 |
Array<int> v1(20);
|
| 42 |
Array<dcomplex> v2(30);
|
| 43 |
|
| 44 |
-
v1[3] = 7;
|
| 45 |
-
v2[3] = dcomplex(7,8);
|
| 46 |
```
|
| 47 |
|
| 48 |
— *end example*]
|
| 49 |
|
|
|
|
| 14 |
T& operator[](int);
|
| 15 |
T& elem(int i) { return v[i]; }
|
| 16 |
};
|
| 17 |
```
|
| 18 |
|
| 19 |
+
declares three member functions of a class template. The subscript
|
| 20 |
+
function might be defined like this:
|
| 21 |
|
| 22 |
``` cpp
|
| 23 |
template<class T> T& Array<T>::operator[](int i) {
|
| 24 |
if (i<0 || sz<=i) error("Array: range error");
|
| 25 |
return v[i];
|
| 26 |
}
|
| 27 |
```
|
| 28 |
|
| 29 |
+
A constrained member function can be defined out of line:
|
| 30 |
+
|
| 31 |
+
``` cpp
|
| 32 |
+
template<typename T> concept C = requires {
|
| 33 |
+
typename T::type;
|
| 34 |
+
};
|
| 35 |
+
|
| 36 |
+
template<typename T> struct S {
|
| 37 |
+
void f() requires C<T>;
|
| 38 |
+
void g() requires C<T>;
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
template<typename T>
|
| 42 |
+
void S<T>::f() requires C<T> { } // OK
|
| 43 |
+
template<typename T>
|
| 44 |
+
void S<T>::g() { } // error: no matching function in S<T>
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
— *end example*]
|
| 48 |
|
| 49 |
The *template-argument*s for a member function of a class template are
|
| 50 |
determined by the *template-argument*s of the type of the object for
|
| 51 |
which the member function is called.
|
| 52 |
|
| 53 |
[*Example 2*:
|
| 54 |
|
| 55 |
+
The *template-argument* for `Array<T>::operator[]` will be determined by
|
| 56 |
+
the `Array` to which the subscripting operation is applied.
|
| 57 |
|
| 58 |
``` cpp
|
| 59 |
Array<int> v1(20);
|
| 60 |
Array<dcomplex> v2(30);
|
| 61 |
|
| 62 |
+
v1[3] = 7; // Array<int>::operator[]
|
| 63 |
+
v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]
|
| 64 |
```
|
| 65 |
|
| 66 |
— *end example*]
|
| 67 |
|