Binary Folds

suggest change

Binary folds are basically unary folds, with an extra argument.

There are 2 kinds of binary folds:

(((Value op Pack1) op Pack2) op ...) op PackN
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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Fold expressions:
* Binary Folds

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
76 Fold expressions
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype