To fold a parameter pack over a binary operator, use one of the following forms:
- Left folding with a unary form (... op pack):
template <typename... Ts>
auto add(Ts... args)
{
return (... + args);
}
- Left folding with a binary form (init op ... op pack):
template <typename... Ts>
auto add_to_one(Ts... args)
{
return (1 + ... + args);
}
- Right folding with a unary form (pack op ...):
template <typename... Ts>
auto add(Ts... args)
{
return (args + ...);
}
- Right folding with a binary form (pack op ... op init):
template <typename... Ts>
auto add_to_one(Ts... args)
{
return (args + ... + 1);
}
The parentheses shown above are part of the fold expression and cannot be omitted.