Binary Folds
suggest changeBinary 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: ((num - a) - b) - c
// A binary right fold, (args - ... - num), is legal but associates
// the other way, so it does not compute num - 5 - 10 - 15.
}
int result = removeFrom(1000, 5, 10, 15); //'result' is 1000 - 5 - 10 - 15 = 970
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents