一元折叠

一元折叠用于在特定运算符上折叠参数包 。有一种一元褶皱:

  • 一元(... op pack) 其扩展如下:

    ((Pack1 op Pack2) op ...) op PackN
    
  • 一元折叠 (pack op ...),扩展如下:

    Pack1 op (... (Pack(N-1) op PackN)) 
    

这是一个例子

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