fork(1) 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. cout << "Constructor with rvalue" << endl;
  22. }
  23.  
  24. void dispatch(){
  25. cout << boolalpha << mFunctor() << endl;
  26. }
  27. };
  28.  
  29. int main() {
  30. Dispatcher dt = Dispatcher(FunctorTrue());
  31. dt.dispatch();
  32. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
Constructor with rvalue
false