Use fold expressions for variadic tuples
The std::tuple
class is essentially a more complex, and less convenient, struct
. The interface for tuple
is cumbersome, although class template argument deduction and structured binding have made it somewhat easier.
I tend to use struct
before tuple
for most applications, with one significant exception: the one real advantage of tuple
is that it can be used with fold expressions in a variadic context.
Fold expressions
Designed to make it easier to expand a variadic parameter pack, fold expressions are a new feature with C++17. Prior to fold expressions, expanding a parameter pack required a recursive function:
template<typename T> void f(T final) { cout << final << '\n'; } template<typename T, typename... Args> void f(T first, Args... args) { cout << first; f(args...); } int main() { f("hello...