fork download
  1. #include <iostream>
  2.  
  3. void doSomething (int x) {std::cout << "Do something with " << x << std::endl;}
  4.  
  5. struct Base {
  6. virtual int foo() const {return 5;}
  7. virtual int goo() const {return 6;}
  8. virtual int hoo() const {return 7;}
  9. void noTemplatePattern() const {
  10. Base(*this).templatePattern();
  11. }
  12. void templatePattern() const {
  13. // Code A
  14. if (foo() < 6) {
  15. // Code B
  16. }
  17. doSomething (goo());
  18. // Code C
  19. if (hoo() > 10) {
  20. // Code D
  21. }
  22. }
  23. };
  24.  
  25. struct Derived : Base {
  26. virtual int foo() const override {return 12;}
  27. virtual int goo() const override {return 13;}
  28. virtual int hoo() const override {return 14;}
  29. };
  30.  
  31. int main() {
  32. Derived d;
  33. d.noTemplatePattern();
  34. d.templatePattern();
  35. }
  36.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Do something with 6
Do something with 13