用recursive的方式來使用c++11的variadic template,有functional programming的味道
latest #13
c++11:
template <typename T>
T Sum(T num) {
return num;
}
template <typename T, typename... Args>
T Sum(T num, Args... args) {
return Sum(args...) + num;
}
auto sum2 = Sum(1, 3, 5, 10, 2);
sum([]) -> 0;
sum([Num|Rest]) -> sum(Rest) + Num.
Sum2 = sum:sum([1, 3, 5, 10, 2]).
back to top