Binary folds are basically unary folds, with an extra argument.
There are 2 kinds of binary folds:
Binary Left Fold - (value op ... op pack)
- Expands as follows:
(((Value op Pack1) op Pack2) op ...) op PackN
Binary Right Fold (pack op ... op value)
- Expands as follows:
Pack1 op (... op (Pack(N-1) op (PackN op Value)))
Here is an example:
template<typename... Ts>
int removeFrom(int num, Ts... args)
{
return (num - ... - args); //Binary left fold
// Note that a binary right fold cannot be used
// due to the lack of associativity of operator-
}
int result = removeFrom(1000, 5, 10, 15); //'result' is 1000 - 5 - 10 - 15 = 970