fork download
  1. #include <string>
  2. #include <iostream>
  3.  
  4. struct StratA {
  5. enum gender { male, female, unknown };
  6. double price(std::string name, int age, gender g) const
  7. { return 42; }
  8. };
  9.  
  10. struct StratB {
  11. double price(int age, int volume, double historic_rate) const
  12. { return (age*age*historic_rate)/volume; }
  13. };
  14.  
  15. template <typename PricingStrategy=StratA>
  16. struct SomeEngine
  17. {
  18. template <typename... Args>
  19. void doSomethingInvolvingPricing(std::string logmessage,
  20. Args... args) // involving pricing
  21. {
  22. std::cout << logmessage << ": " << PricingStrategy().price(std::forward<Args>(args)...) << '\n';
  23. }
  24. };
  25.  
  26. int main()
  27. {
  28. SomeEngine<>().doSomethingInvolvingPricing("default", "name", 18, StratA::female);
  29. SomeEngine<StratB>().doSomethingInvolvingPricing("overridden", 18, 3000, 4.5);
  30. }
  31.  
Success #stdin #stdout 0s 3016KB
stdin
Standard input is empty
stdout
default: 42
overridden: 0.486