tmp/tmpex3biw7x/{from.md → to.md}
RENAMED
|
@@ -1,13 +1,16 @@
|
|
| 1 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
| 2 |
|
| 3 |
-
A class
|
| 4 |
-
set of related types.
|
| 5 |
-
common definition for list of `int`, list of `float`, and list of
|
| 6 |
-
pointers to `Shape`s.
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
``` cpp
|
| 11 |
template<class T> class Array {
|
| 12 |
T* v;
|
| 13 |
int sz;
|
|
@@ -16,14 +19,16 @@ public:
|
|
| 16 |
T& operator[](int);
|
| 17 |
T& elem(int i) { return v[i]; }
|
| 18 |
};
|
| 19 |
```
|
| 20 |
|
| 21 |
-
The prefix `template
|
| 22 |
-
declared and that a *type-name* `T`
|
| 23 |
other words, `Array` is a parameterized type with `T` as its parameter.
|
| 24 |
|
|
|
|
|
|
|
| 25 |
When a member function, a member class, a member enumeration, a static
|
| 26 |
data member or a member template of a class template is defined outside
|
| 27 |
of the class template definition, the member definition is defined as a
|
| 28 |
template definition in which the *template-parameter*s are those of the
|
| 29 |
class template. The names of the template parameters used in the
|
|
@@ -32,10 +37,12 @@ names used in the class template definition. The template argument list
|
|
| 32 |
following the class template name in the member definition shall name
|
| 33 |
the parameters in the same order as the one used in the template
|
| 34 |
parameter list of the member. Each template parameter pack shall be
|
| 35 |
expanded with an ellipsis in the template argument list.
|
| 36 |
|
|
|
|
|
|
|
| 37 |
``` cpp
|
| 38 |
template<class T1, class T2> struct A {
|
| 39 |
void f1();
|
| 40 |
void f2();
|
| 41 |
};
|
|
@@ -52,20 +59,24 @@ template<class ... Types> struct B {
|
|
| 52 |
|
| 53 |
template<class ... Types> void B<Types ...>::f3() { } // OK
|
| 54 |
template<class ... Types> void B<Types>::f4() { } // error
|
| 55 |
```
|
| 56 |
|
|
|
|
|
|
|
| 57 |
In a redeclaration, partial specialization, explicit specialization or
|
| 58 |
explicit instantiation of a class template, the *class-key* shall agree
|
| 59 |
in kind with the original class template declaration (
|
| 60 |
[[dcl.type.elab]]).
|
| 61 |
|
| 62 |
#### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
|
| 63 |
|
| 64 |
A member function of a class template may be defined outside of the
|
| 65 |
class template definition in which it is declared.
|
| 66 |
|
|
|
|
|
|
|
| 67 |
``` cpp
|
| 68 |
template<class T> class Array {
|
| 69 |
T* v;
|
| 70 |
int sz;
|
| 71 |
public:
|
|
@@ -83,46 +94,60 @@ template<class T> T& Array<T>::operator[](int i) {
|
|
| 83 |
if (i<0 || sz<=i) error("Array: range error");
|
| 84 |
return v[i];
|
| 85 |
}
|
| 86 |
```
|
| 87 |
|
|
|
|
|
|
|
| 88 |
The *template-argument*s for a member function of a class template are
|
| 89 |
determined by the *template-argument*s of the type of the object for
|
| 90 |
-
which the member function is called.
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
``` cpp
|
| 95 |
Array<int> v1(20);
|
| 96 |
Array<dcomplex> v2(30);
|
| 97 |
|
| 98 |
v1[3] = 7; // Array<int>::operator[]()
|
| 99 |
v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]()
|
| 100 |
```
|
| 101 |
|
|
|
|
|
|
|
| 102 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 103 |
|
| 104 |
A member class of a class template may be defined outside the class
|
| 105 |
-
template definition in which it is declared.
|
| 106 |
-
|
| 107 |
-
[
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
``` cpp
|
| 110 |
template<class T> struct A {
|
| 111 |
class B;
|
| 112 |
};
|
| 113 |
A<int>::B* b1; // OK: requires A to be defined but not A::B
|
| 114 |
template<class T> class A<T>::B { };
|
| 115 |
A<int>::B b2; // OK: requires A::B to be defined
|
| 116 |
```
|
| 117 |
|
|
|
|
|
|
|
| 118 |
#### Static data members of class templates <a id="temp.static">[[temp.static]]</a>
|
| 119 |
|
| 120 |
A definition for a static data member or static data member template may
|
| 121 |
be provided in a namespace scope enclosing the definition of the static
|
| 122 |
member’s class template.
|
| 123 |
|
|
|
|
|
|
|
| 124 |
``` cpp
|
| 125 |
template<class T> class X {
|
| 126 |
static T s;
|
| 127 |
};
|
| 128 |
template<class T> T X<T>::s = 0;
|
|
@@ -134,30 +159,40 @@ struct limits {
|
|
| 134 |
|
| 135 |
template<class T>
|
| 136 |
const T limits::min = { }; // definition
|
| 137 |
```
|
| 138 |
|
|
|
|
|
|
|
| 139 |
An explicit specialization of a static data member declared as an array
|
| 140 |
of unknown bound can have a different bound from its definition, if any.
|
| 141 |
|
|
|
|
|
|
|
| 142 |
``` cpp
|
| 143 |
template <class T> struct A {
|
| 144 |
static int i[];
|
| 145 |
};
|
| 146 |
template <class T> int A<T>::i[4]; // 4 elements
|
| 147 |
template <> int A<int>::i[] = { 1 }; // OK: 1 element
|
| 148 |
```
|
| 149 |
|
|
|
|
|
|
|
| 150 |
#### Enumeration members of class templates <a id="temp.mem.enum">[[temp.mem.enum]]</a>
|
| 151 |
|
| 152 |
An enumeration member of a class template may be defined outside the
|
| 153 |
class template definition.
|
| 154 |
|
|
|
|
|
|
|
| 155 |
``` cpp
|
| 156 |
template<class T> struct A {
|
| 157 |
enum E : T;
|
| 158 |
};
|
| 159 |
A<int> a;
|
| 160 |
template<class T> enum A<T>::E : T { e1, e2 };
|
| 161 |
A<int>::E e = A<int>::e1;
|
| 162 |
```
|
| 163 |
|
|
|
|
|
|
|
|
|
| 1 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
| 2 |
|
| 3 |
+
A *class template* defines the layout and operations for an unbounded
|
| 4 |
+
set of related types.
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
[*Example 1*:
|
| 7 |
+
|
| 8 |
+
A single class template `List` might provide an unbounded set of class
|
| 9 |
+
definitions: one class `List<T>` for every type `T`, each describing a
|
| 10 |
+
linked list of elements of type `T`. Similarly, a class template `Array`
|
| 11 |
+
describing a contiguous, dynamic array might be defined like this:
|
| 12 |
|
| 13 |
``` cpp
|
| 14 |
template<class T> class Array {
|
| 15 |
T* v;
|
| 16 |
int sz;
|
|
|
|
| 19 |
T& operator[](int);
|
| 20 |
T& elem(int i) { return v[i]; }
|
| 21 |
};
|
| 22 |
```
|
| 23 |
|
| 24 |
+
The prefix `template<class T>` specifies that a template is being
|
| 25 |
+
declared and that a *type-name* `T` may be used in the declaration. In
|
| 26 |
other words, `Array` is a parameterized type with `T` as its parameter.
|
| 27 |
|
| 28 |
+
— *end example*]
|
| 29 |
+
|
| 30 |
When a member function, a member class, a member enumeration, a static
|
| 31 |
data member or a member template of a class template is defined outside
|
| 32 |
of the class template definition, the member definition is defined as a
|
| 33 |
template definition in which the *template-parameter*s are those of the
|
| 34 |
class template. The names of the template parameters used in the
|
|
|
|
| 37 |
following the class template name in the member definition shall name
|
| 38 |
the parameters in the same order as the one used in the template
|
| 39 |
parameter list of the member. Each template parameter pack shall be
|
| 40 |
expanded with an ellipsis in the template argument list.
|
| 41 |
|
| 42 |
+
[*Example 2*:
|
| 43 |
+
|
| 44 |
``` cpp
|
| 45 |
template<class T1, class T2> struct A {
|
| 46 |
void f1();
|
| 47 |
void f2();
|
| 48 |
};
|
|
|
|
| 59 |
|
| 60 |
template<class ... Types> void B<Types ...>::f3() { } // OK
|
| 61 |
template<class ... Types> void B<Types>::f4() { } // error
|
| 62 |
```
|
| 63 |
|
| 64 |
+
— *end example*]
|
| 65 |
+
|
| 66 |
In a redeclaration, partial specialization, explicit specialization or
|
| 67 |
explicit instantiation of a class template, the *class-key* shall agree
|
| 68 |
in kind with the original class template declaration (
|
| 69 |
[[dcl.type.elab]]).
|
| 70 |
|
| 71 |
#### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
|
| 72 |
|
| 73 |
A member function of a class template may be defined outside of the
|
| 74 |
class template definition in which it is declared.
|
| 75 |
|
| 76 |
+
[*Example 1*:
|
| 77 |
+
|
| 78 |
``` cpp
|
| 79 |
template<class T> class Array {
|
| 80 |
T* v;
|
| 81 |
int sz;
|
| 82 |
public:
|
|
|
|
| 94 |
if (i<0 || sz<=i) error("Array: range error");
|
| 95 |
return v[i];
|
| 96 |
}
|
| 97 |
```
|
| 98 |
|
| 99 |
+
— *end example*]
|
| 100 |
+
|
| 101 |
The *template-argument*s for a member function of a class template are
|
| 102 |
determined by the *template-argument*s of the type of the object for
|
| 103 |
+
which the member function is called.
|
| 104 |
+
|
| 105 |
+
[*Example 2*:
|
| 106 |
+
|
| 107 |
+
The *template-argument* for `Array<T>::operator[]()` will be determined
|
| 108 |
+
by the `Array` to which the subscripting operation is applied.
|
| 109 |
|
| 110 |
``` cpp
|
| 111 |
Array<int> v1(20);
|
| 112 |
Array<dcomplex> v2(30);
|
| 113 |
|
| 114 |
v1[3] = 7; // Array<int>::operator[]()
|
| 115 |
v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]()
|
| 116 |
```
|
| 117 |
|
| 118 |
+
— *end example*]
|
| 119 |
+
|
| 120 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 121 |
|
| 122 |
A member class of a class template may be defined outside the class
|
| 123 |
+
template definition in which it is declared.
|
| 124 |
+
|
| 125 |
+
[*Note 1*:
|
| 126 |
+
|
| 127 |
+
The member class must be defined before its first use that requires an
|
| 128 |
+
instantiation ([[temp.inst]]). For example,
|
| 129 |
|
| 130 |
``` cpp
|
| 131 |
template<class T> struct A {
|
| 132 |
class B;
|
| 133 |
};
|
| 134 |
A<int>::B* b1; // OK: requires A to be defined but not A::B
|
| 135 |
template<class T> class A<T>::B { };
|
| 136 |
A<int>::B b2; // OK: requires A::B to be defined
|
| 137 |
```
|
| 138 |
|
| 139 |
+
— *end note*]
|
| 140 |
+
|
| 141 |
#### Static data members of class templates <a id="temp.static">[[temp.static]]</a>
|
| 142 |
|
| 143 |
A definition for a static data member or static data member template may
|
| 144 |
be provided in a namespace scope enclosing the definition of the static
|
| 145 |
member’s class template.
|
| 146 |
|
| 147 |
+
[*Example 1*:
|
| 148 |
+
|
| 149 |
``` cpp
|
| 150 |
template<class T> class X {
|
| 151 |
static T s;
|
| 152 |
};
|
| 153 |
template<class T> T X<T>::s = 0;
|
|
|
|
| 159 |
|
| 160 |
template<class T>
|
| 161 |
const T limits::min = { }; // definition
|
| 162 |
```
|
| 163 |
|
| 164 |
+
— *end example*]
|
| 165 |
+
|
| 166 |
An explicit specialization of a static data member declared as an array
|
| 167 |
of unknown bound can have a different bound from its definition, if any.
|
| 168 |
|
| 169 |
+
[*Example 2*:
|
| 170 |
+
|
| 171 |
``` cpp
|
| 172 |
template <class T> struct A {
|
| 173 |
static int i[];
|
| 174 |
};
|
| 175 |
template <class T> int A<T>::i[4]; // 4 elements
|
| 176 |
template <> int A<int>::i[] = { 1 }; // OK: 1 element
|
| 177 |
```
|
| 178 |
|
| 179 |
+
— *end example*]
|
| 180 |
+
|
| 181 |
#### Enumeration members of class templates <a id="temp.mem.enum">[[temp.mem.enum]]</a>
|
| 182 |
|
| 183 |
An enumeration member of a class template may be defined outside the
|
| 184 |
class template definition.
|
| 185 |
|
| 186 |
+
[*Example 1*:
|
| 187 |
+
|
| 188 |
``` cpp
|
| 189 |
template<class T> struct A {
|
| 190 |
enum E : T;
|
| 191 |
};
|
| 192 |
A<int> a;
|
| 193 |
template<class T> enum A<T>::E : T { e1, e2 };
|
| 194 |
A<int>::E e = A<int>::e1;
|
| 195 |
```
|
| 196 |
|
| 197 |
+
— *end example*]
|
| 198 |
+
|