Unary Folds

suggest change

Unary folds are used to fold parameter packs over a specific operator. There are 2 kinds of unary folds:

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

Feedback about page:

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



Table Of Contents