在CL裡我們可以這樣:
$ sbcl * (+ 1 2 3) 6 * (< 1 2 3) T * (< 2 3 1) NIL *
從簡單的方面看, CL的+和<就是一個接收多參數的函數,有點類似cpp的add(1,2,3)和less(1,2,3)這樣.
所以當C++11開始有了變參模板以後, 就可以玩多參數的加法和多參數比較了
#include <functional>
template<typename O, typename A,typename B>
bool cmp(O o, A a,B b){
return o(a, b);
}
template<typename O, typename A,typename B,typename... C>
bool cmp(O o,A a,B b,C... c){
return o(a, b) and cmp(o,b,c...);
}
template<typename O, typename A,typename B>
A reduce(O o, A a,B b){
return o(a, b);
}
template<typename O, typename A,typename B,typename... C>
A reduce(O o,A a,B b,C... c){
return reduce(o,o(a, b),c...);
}
bool foo(int a,int b,int c,int d){
return cmp(std::less<int>(), a,b,c,d);
}
int bar(int a,int b,int c,int d){
return reduce(std::plus<int>(), a,b,c,d);
}