fork download
  1. #include <iostream>
  2.  
  3. template <typename Child>
  4. struct Base
  5. {
  6. void interface()
  7. {
  8. static_cast<Child*>(this)->implementation();
  9. }
  10. };
  11.  
  12. struct Derived : Base<Derived>
  13. {
  14. void implementation()
  15. {
  16. std::clog << "Derived implementation\n";
  17. }
  18. };
  19.  
  20. int main()
  21. {
  22. Derived d;
  23. d.interface(); // Prints "Derived implementation"
  24.  
  25. Base<Derived> & statically_polymorphic = d;
  26. statically_polymorphic.interface(); // Prints "Derived implementation" as well
  27. }
  28.  
  29.  
Success #stdin #stdout 0.01s 2720KB
stdin
Standard input is empty
stdout
Standard output is empty