fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. template<typename T, typename U>
  5. class BinaryFunctor
  6. {
  7. public:
  8. BinaryFunctor(std::function<U(T,T)> a, std::function<U(T,T)> b): a_(a), b_(b) {}
  9.  
  10. bool operator()(T left, T right)
  11. {
  12. return a_(left, right) < b_(left, right);
  13. }
  14.  
  15. private:
  16. std::function<U(T,T)> a_;
  17. std::function<U(T,T)> b_;
  18. };
  19.  
  20. int plus(int l, int r) { return l + r; }
  21. int minus(int l, int r) { return l - r; }
  22.  
  23. int main(int argc, char* argv[])
  24. {
  25. BinaryFunctor<int, int> f(plus, minus);
  26. std::cout << f(1, 2) << std::endl;
  27.  
  28. std::cin.get();
  29. return 0;
  30. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:8: error: expected `)' before ‘<’ token
prog.cpp:16: error: ISO C++ forbids declaration of ‘function’ with no type
prog.cpp:16: error: invalid use of ‘::’
prog.cpp:16: error: expected ‘;’ before ‘<’ token
prog.cpp:17: error: ISO C++ forbids declaration of ‘function’ with no type
prog.cpp:17: error: invalid use of ‘::’
prog.cpp:17: error: expected ‘;’ before ‘<’ token
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:25: error: no matching function for call to ‘BinaryFunctor<int, int>::BinaryFunctor(int (&)(int, int), int (&)(int, int))’
prog.cpp:6: note: candidates are: BinaryFunctor<int, int>::BinaryFunctor()
prog.cpp:6: note:                 BinaryFunctor<int, int>::BinaryFunctor(const BinaryFunctor<int, int>&)
prog.cpp: In member function ‘bool BinaryFunctor<T, U>::operator()(T, T) [with T = int, U = int]’:
prog.cpp:26:   instantiated from here
prog.cpp:12: error: ‘b_’ was not declared in this scope
prog.cpp:12: error: ‘a_’ was not declared in this scope
stdout
Standard output is empty