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 foo(std::false_type());}
  7. virtual int goo() const {return goo(std::false_type());}
  8. virtual int hoo() const {return hoo(std::false_type());}
  9.  
  10. void noTemplatePattern() const { doIt (std::false_type()); }
  11. void templatePattern() const { doIt (std::true_type()); }
  12.  
  13. private:
  14. template <typename T>
  15. void doIt (T t) const {
  16. // Code A
  17. if (foo(t) < 6) {
  18. // Code B
  19. }
  20. doSomething (goo(t));
  21. // Code C
  22. if (hoo(t) > 10) {
  23. // Code D
  24. }
  25. }
  26. int foo(std::false_type) const {return 5;}
  27. int goo(std::false_type) const {return 6;}
  28. int hoo(std::false_type) const {return 7;}
  29. int foo(std::true_type) const {return foo();}
  30. int goo(std::true_type) const {return goo();}
  31. int hoo(std::true_type) const {return hoo();}
  32. };
  33.  
  34. struct Derived : Base {
  35. virtual int foo() const override {return 12;}
  36. virtual int goo() const override {return 13;}
  37. virtual int hoo() const override {return 14;}
  38. };
  39.  
  40. int main() {
  41. Derived d;
  42. d.noTemplatePattern();
  43. d.templatePattern();
  44. }
  45.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Do something with 6
Do something with 13