fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Foo {
  6. public:
  7. template <typename Type> Type get();
  8.  
  9. private:
  10. int value = 10;
  11. };
  12.  
  13. template <> int Foo::get() { return value; }
  14. template <> string Foo::get() { return to_string(value); }
  15.  
  16. class Bar {
  17. public:
  18. template <typename Type> Type get();
  19.  
  20. private:
  21. int value = 15;
  22. };
  23.  
  24. template <> int Bar::get() { return value; }
  25. template <> string Bar::get() { return to_string(value); }
  26.  
  27. class Elo {
  28. public:
  29. template <typename Type, typename F> Type get();
  30.  
  31. private:
  32. Foo foo;
  33. Bar bar;
  34. };
  35.  
  36. template <> int Elo::get<int, Foo>() { return foo.get<int>(); }
  37. template <> string Elo::get<string, Foo>() { return foo.get<string>(); }
  38. template <> int Elo::get<int, Bar>() { return bar.get<int>(); }
  39. template <> string Elo::get<string, Bar>() { return bar.get<string>(); }
  40.  
  41. int main() {
  42. Elo e;
  43. cout << e.get<int, Foo>() << endl;
  44. cout << e.get<int, Bar>() << endl;
  45. cout << e.get<string, Foo>() << endl;
  46. cout << e.get<string, Bar>() << endl;
  47. // cout<<e.get<int,Elo>()<<endl; blad kompilacji
  48. return 0;
  49. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
10
15
10
15