fork download
  1. #include <iostream>
  2.  
  3. template<typename T>
  4. struct CRTP_base
  5. {
  6. void execute()
  7. {
  8. static_cast<T*>(this)->f();
  9. }
  10. };
  11.  
  12. struct Foo : public CRTP_base<Foo>
  13. {
  14. void f()
  15. {
  16. std::cout << "Wow, compile-time resolved polymorphism!" << std::endl;
  17. }
  18. };
  19.  
  20. int main()
  21. {
  22. Foo my_foo_instance;
  23. CRTP_base<Foo>& base_reference = my_foo_instance;
  24. base_reference.execute();
  25. };
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Wow, compile-time resolved polymorphism!