fork download
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iterator>
  4. #include <vector>
  5.  
  6. template <typename ContainerOut, typename ContainerIn, typename F>
  7. ContainerOut Map( const F& f, const ContainerIn& xs )
  8. {
  9. ContainerOut ys;
  10. // For performance reasons one would use
  11. // ys.reserve( xs.size() )
  12. // and std::back_inserter instead of std::inserter
  13. // if ys is a std::vector.
  14. auto it = std::inserter( ys, end( ys ) );
  15. std::transform( begin( xs ), end( xs ), it, f );
  16. return ys;
  17. }
  18.  
  19. template <typename Ret, typename Arg1, typename ...Args>
  20. auto Curry( Ret f(Arg1, Args...), Arg1 arg ) -> std::function<Ret(Args...)>
  21. {
  22. return [=]( Args ...args ) { return f( arg, args... ); };
  23. }
  24.  
  25. int main()
  26. {
  27. auto square = [](int x){ return x*x; };
  28.  
  29. typedef std::vector<int> Row;
  30. typedef std::vector<Row> Mat;
  31. Mat mat;
  32. auto squareElements = Curry(Map<Row>, square);
  33. Mat squaredMat = Map<Mat>(squareElements, mat);
  34. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:32:49: error: no matching function for call to 'Curry(<unresolved overloaded function type>, main()::<lambda(int)>&)'
     auto squareElements = Curry(Map<Row>, square);
                                                 ^
prog.cpp:20:6: note: candidate: template<class Ret, class Arg1, class ... Args> std::function<Ret(Args ...)> Curry(Ret (*)(Arg1, Args ...), Arg1)
 auto Curry( Ret f(Arg1, Args...), Arg1 arg ) -> std::function<Ret(Args...)>
      ^
prog.cpp:20:6: note:   template argument deduction/substitution failed:
prog.cpp:32:49: note:   couldn't deduce template parameter 'Ret'
     auto squareElements = Curry(Map<Row>, square);
                                                 ^
stdout
Standard output is empty