Unary Folds
suggest changeUnary folds are used to fold parameter packs over a specific operator. There are 2 kinds of unary folds:
- Unary Left Fold
(... op pack)
which expands as follows:
((Pack1 op Pack2) op ...) op PackN
- Unary Right Fold
(pack op ...)
which expands as follows:
Pack1 op (... (Pack(N-1) op PackN))
Here is an example
template<typename... Ts>
int sum(Ts... args)
{
return (... + args); //Unary left fold
//return (args + ...); //Unary right fold
// The two are equivalent if the operator is associative.
// For +, ((1+2)+3) (left fold) == (1+(2+3)) (right fold)
// For -, ((1-2)-3) (left fold) != (1-(2-3)) (right fold)
}
int result = sum(1, 2, 3); // 6
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents