
Folding Expression 1/2
C++17 Folding expressions perform a fold of a template parameter pack over any
binary operator in C++ ( + , * , , , += , && , <= etc.)
Unary/Binary folding
template<typename... Args>
auto add_unary(Args... args) { // Unary folding
return (... + args); // unfold: 1 + 2.0f + 3ull
}
template<typename... Args>
auto add_binary(Args... args) { // Binary folding
return (1 + ... + args); // unfold: 1 + 1 + 2.0f + 3ull
} // the value on the left of the ellipsis is typically the identity
add_unary(1, 2.0f, 3ll); // returns 6.0f (float)
add_binary(1, 2.0f, 3ll); // returns 7.0f (float)
63/85