fork download
  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;
  4.  
  5. class Functor : public std::function<bool()> {
  6. public:
  7. virtual ~Functor() {};
  8. virtual bool operator()() const {return false;};
  9. };
  10.  
  11. class FunctorTrue : public Functor{
  12. public:
  13. bool operator()() const {return true;}
  14. };
  15.  
  16. class Dispatcher {
  17. public:
  18. const Functor & mFunctor;
  19. Dispatcher(const Functor & functor): mFunctor(functor) {}
  20. Dispatcher(const Functor && functor): mFunctor(functor) {
  21. }
  22.  
  23. void dispatch(){
  24. cout << boolalpha << mFunctor() << endl;
  25. }
  26. };
  27.  
  28. int main() {
  29. Dispatcher dt = Dispatcher(FunctorTrue());
  30. dt.dispatch();
  31. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
true