-
Notifications
You must be signed in to change notification settings - Fork 0
/
left-fold.cc
36 lines (31 loc) · 915 Bytes
/
left-fold.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <utility>
#include <functional>
template <class BinOp, class... Arg>
struct left_fold_t;
template <class BinOp, class Arg>
struct left_fold_t<BinOp, Arg> {
constexpr static Arg call(BinOp f, Arg a)
{
return a;
}
};
template <class BinOp, class First, class Second, class... Rest>
struct left_fold_t<BinOp, First, Second, Rest...> {
constexpr static auto call(BinOp f, First fs, Second sc, Rest... rest)
{
return left_fold_t<BinOp, decltype(f(fs, sc)), Rest...>::call(
f,
f(std::forward<First>(fs), std::forward<Second>(sc)),
std::forward<Rest>(rest)...);
}
};
template <class BinOp, class... Arg>
constexpr auto left_fold(BinOp f, Arg... arg)
-> decltype(left_fold_t<BinOp, Arg...>::call(f, arg...))
{
return left_fold_t<BinOp, Arg...>::call(f, std::forward<Arg>(arg)...);
}
int main(int argc, char *argv[])
{
return left_fold(std::plus<int>(), 1, 2, 3);
}