zhangsongcui

Param carrying

Jun 24th, 2012
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. template <typename TFunc>
  5. struct carry;
  6.  
  7. template <typename TResult, typename TFirstParam, typename... TOthers>
  8. struct carry<TResult (TFirstParam, TOthers...)> {
  9.     carry(std::function<TResult (TFirstParam, TOthers...)> f_): f(move(f_)) {}
  10.  
  11.     auto operator ()(TFirstParam para) -> carry<TResult (TOthers...)> {
  12.         return carry<TResult (TOthers...)>([=] (TOthers... args) {
  13.             return f(para, args...);
  14.         });
  15.     }
  16.  
  17.     std::function<TResult (TFirstParam, TOthers...)> f;
  18. };
  19.  
  20. template <typename TResult, typename TFirstParam>
  21. struct carry<TResult (TFirstParam)> {
  22.     carry(std::function<TResult (TFirstParam)> f_): f(move(f_)) {}
  23.  
  24.     TResult operator ()(TFirstParam para) {
  25.         return f(para);
  26.     }
  27.  
  28.     std::function<TResult (TFirstParam)> f;
  29. };
  30.  
  31. int main(void)
  32. {
  33.     using namespace std;
  34.     carry<int(int, int, int, int)> f([] (int a, int b, int c, int d) { return a+b+c+d; });
  35.     cout << f(1)(2)(3)(4);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment