fork(2) download
  1. #include <iostream>
  2.  
  3. struct X
  4. {
  5. template <typename F>
  6. auto accept(F && f)
  7. {
  8. return [this, &f](auto &&... args) {
  9. return f(this, std::forward<decltype(args)>(args)...); };
  10. }
  11.  
  12. virtual void foo() const { std::cout << "base\n"; }
  13. };
  14.  
  15. struct Y : X
  16. {
  17. void foo() const override { std::cout << "derived\n"; }
  18. };
  19.  
  20.  
  21. int main()
  22. {
  23. Y y;
  24. X * p = &y;
  25.  
  26. (p->*(&X::foo))();
  27.  
  28. p->accept([](X * that){that->X::foo();});
  29. }
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
derived