fork download
  1. #include <type_traits>
  2.  
  3. struct A { int x; };
  4. struct B { int x; };
  5. struct C { int x; };
  6. struct D { int x; };
  7.  
  8. template <class ... Params> class Parameter { };
  9.  
  10. template <class Param>
  11. class Parameter<Param> : public Param
  12. {
  13. public:
  14. //! Get a parameter
  15. template <class Param2>
  16. typename std::enable_if<std::is_same<Param, Param2>::value, int>::type
  17. getParam() const
  18. { return Param::x; }
  19. };
  20.  
  21. template <class Param, class ... Tail>
  22. class Parameter<Param, Tail ...> : public Param, public Parameter<Tail ...>
  23. {
  24. public:
  25. using Parameter<Tail ...>::getParam;
  26. //! Get a parameter
  27. template <class Param2>
  28. typename std::enable_if<std::is_same<Param, Param2>::value, int>::type
  29. getParam() const
  30. { return Param::x; }
  31. };
  32.  
  33. class Base : public Parameter<A, B>
  34. { };
  35.  
  36. class Derived : public Base, public Parameter<C, D>
  37. {
  38. public:
  39. using Base::getParam;
  40. using Parameter<C, D>::getParam;
  41. };
  42.  
  43. int main(int const argc, char const * argv[])
  44. {
  45. Base base;
  46. int a = base.getParam<A>(); // ok
  47. int b = base.getParam<B>(); // ok
  48.  
  49. Derived derived;
  50. int c0 = derived.getParam<C>();
  51. int c1 = derived.Derived::getParam<C>();
  52. int c2 = derived.Parameter<C, D>::getParam<C>();
  53.  
  54. int a0 = derived.getParam<A>();
  55. int a1 = derived.Base::getParam<A>();
  56. int a2 = derived.Parameter<A, B>::getParam<A>();
  57.  
  58. return 0;
  59. }
  60.  
Success #stdin #stdout 0s 3292KB
stdin
Standard input is empty
stdout
Standard output is empty