mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 04:34:40 +03:00
book: remove default template parameters (#194)
This commit is contained in:
@@ -709,28 +709,6 @@ int main() {
|
||||
}
|
||||
```
|
||||
|
||||
### Default template parameters
|
||||
|
||||
We may have defined an addition function:
|
||||
|
||||
```cpp
|
||||
template<typename T, typename U>
|
||||
auto add(T x, U y) -> decltype(x+y) {
|
||||
return x+y;
|
||||
}
|
||||
```
|
||||
|
||||
However, when used, it is found that to use add, you must specify the type of its template parameters each time.
|
||||
|
||||
A convenience is provided in C++11 to specify the default parameters of the template:
|
||||
|
||||
```cpp
|
||||
template<typename T = int, typename U = int>
|
||||
auto add(T x, U y) -> decltype(x+y) {
|
||||
return x+y;
|
||||
}
|
||||
```
|
||||
|
||||
### Variadic templates
|
||||
|
||||
The template has always been one of C++'s unique **Black Magic**.
|
||||
|
||||
Reference in New Issue
Block a user