You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Any built-in or user-defined type can be used as a type argument. For example, you can use std::vector in the Standard Library to store ints, doubles, strings, MyClass, constMyClass*, MyClass&. The primary restriction when using templates is that a type argument must support any operations that are applied to the type parameters. For example, if we call minimum using MyClass as in this example:
77
+
Any built-in or user-defined type can be used as a type argument. For example, you can use [std::vector](../standard-library/vector-class.md) in the Standard Library to store variables of type **int**, **double**, [std::string](../standard-library/basic-string-class.md), `MyClass`, **const**`MyClass`*, `MyClass&`, and so on. The primary restriction when using templates is that a type argument must support any operations that are applied to the type parameters. For example, if we call `minimum` using `MyClass` as in this example:
78
78
79
79
```cpp
80
80
classMyClass
@@ -92,7 +92,7 @@ int main()
92
92
}
93
93
```
94
94
95
-
A compiler error will be generated because MyClass does not provide an overload for the < operator.
95
+
A compiler error will be generated because `MyClass` does not provide an overload for the **<** operator.
96
96
97
97
There is no inherent requirement that the type arguments for any particular template all belong to the same object hierarchy, although you can define a template that enforces such a restriction. You can combine object-oriented techniques with templates; for example, you can store a Derived* in a vector\<Base\*>. Note that the arguments must be pointers
98
98
@@ -106,11 +106,11 @@ vector<MyClass*> vec;
106
106
vec2.push_back(make_shared<MyDerived>());
107
107
```
108
108
109
-
The basic requirements that vector and other standard library containers impose on elements of `T` is that `T` be copy-assignable and copy-constructible.
109
+
The basic requirements that `std::vector` and other standard library containers impose on elements of `T` is that `T` be copy-assignable and copy-constructible.
110
110
111
111
## Non-type parameters
112
112
113
-
Unlike generic types in other languages such as C# and Java, C++ templates support non-type parameters, also called value parameters. For example, you can provide a constant integral value to specify the length of an array, as with this example that is similar to the std::array class in the Standard Library:
113
+
Unlike generic types in other languages such as C# and Java, C++ templates support *non-type parameters*, also called value parameters. For example, you can provide a constant integral value to specify the length of an array, as with this example that is similar to the [std::array](../standard-library/array-class.md) class in the Standard Library:
114
114
115
115
```cpp
116
116
template<typename T, size_t L>
@@ -122,14 +122,26 @@ public:
122
122
};
123
123
```
124
124
125
-
Note the syntax in the template declaration. The size_t value is passed in as a template argument at compile time and must be constant or a constexpr expression. You use it like this:
125
+
Note the syntax in the template declaration. The `size_t` value is passed in as a template argument at compile time and must be **const** or a **constexpr** expression. You use it like this:
126
126
127
127
```cpp
128
128
MyArray<MyClass*, 10> arr;
129
129
```
130
130
131
131
Other kinds of values including pointers and references can be passed in as non-type parameters. For example, you can pass in a pointer to a function or function object to customize some operation inside the template code.
132
132
133
+
### Type deduction for non-type template parameters
134
+
135
+
In Visual Studio 2017 and later, in **/std:c++17** mode the compiler deduces the type of a non-type template argument that is declared with **auto**:
136
+
137
+
```cpp
138
+
template <auto x> constexprauto constant = x;
139
+
140
+
auto v1 = constant<5>; // v1 == 5, decltype(v1) is int
141
+
auto v2 = constant<true>; // v2 == true, decltype(v2) is bool
142
+
auto v3 = constant<'a'>; // v3 == 'a', decltype(v3) is char
143
+
```
144
+
133
145
## <aid="template_parameters"></a> Templates as template parameters
134
146
135
147
A template can be a template parameter. In this example, MyClass2 has two template parameters: a typename parameter *T* and a template parameter *Arr*:
0 commit comments