二进制折叠
二进制折叠基本上是一元折叠 ,有一个额外的参数。
有两种二进制折叠:
-
二进制左折 -
(value op ... op pack)
- 扩展如下:(((Value op Pack1) op Pack2) op ...) op PackN
-
Binary Right Fold
(pack op ... op value)
- 扩展如下:Pack1 op (... op (Pack(N-1) op (PackN op Value)))
这是一个例子:
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