一元摺疊
一元摺疊用於在特定運算子上摺疊引數包 。有一種一元褶皺:
-
一元左折
(... 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