Parameter Pack to Initializer List (#65)

* Add alternate use variadic template example
This commit is contained in:
Matthew Guidry
2019-10-24 17:40:38 -05:00
committed by Anthony Calandra
parent cdcd516ae8
commit 4eb1a3ff5f
2 changed files with 26 additions and 0 deletions

View File

@@ -128,6 +128,19 @@ static_assert(arity<>::value == 0);
static_assert(arity<char, short, int>::value == 3);
```
An interesting use for this is creating an _initializer list_ from a _parameter pack_ in order to iterate over variadic function arguments.
```c++
template <typename First, typename... Args>
auto sum(const First first, const Args... args) -> decltype(first) {
const auto values = {first, args...};
return std::accumulate(values.begin(), values.end(), First{0});
}
sum(1, 2, 3, 4, 5); // 15
sum(1, 2, 3); // 6
sum(1.5, 2.0, 3.7); // 7.2
```
### Initializer lists
A lightweight array-like container of elements created using a "braced list" syntax. For example, `{ 1, 2, 3 }` creates a sequences of integers, that has type `std::initializer_list<int>`. Useful as a replacement to passing a vector of objects to a function.
```c++