fork download
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. class A
  5. {
  6. public:
  7. A(T t) : mT(t) {}
  8. virtual ~A() {}
  9. virtual void doThings() = 0;
  10. protected:
  11. T mT;
  12. };
  13.  
  14. template <typename T, typename A = A<T>>
  15. class B : public A
  16. {
  17. public:
  18. B(T t) : A(t) {}
  19. virtual ~B() {}
  20. virtual void doThings() { std::cout << "B" << std::endl; std::cout << A::mT << std::endl; }
  21. };
  22.  
  23. template <typename T, typename A = A<T>>
  24. class C : public A
  25. {
  26. public:
  27. C(T t) : A(t) {}
  28. virtual ~C() {}
  29. virtual void doThings() { std::cout << "C" << std::endl; std::cout << A::mT << std::endl;}
  30. };
  31.  
  32. template <typename... T>
  33. void ignore(T&&... t)
  34. {
  35. }
  36.  
  37. template <typename T, typename ...Args>
  38. class ChildGenerator : public Args...
  39. {
  40. public:
  41. ChildGenerator(T t) : Args(t)... {}
  42.  
  43. // The unpacking of the variadic template does not work here.
  44. // Do I need to make it recursive somehow? How can I do that without having to instantiate new classes B and C?
  45. void doThings() override { ((Args::doThings()) , ...);}
  46. };
  47.  
  48. int main()
  49. {
  50. using B = B<double>;
  51. using C = C<double>;
  52. B c1(0.0);
  53. C c2(1.0);
  54. ChildGenerator<double, B, C> c3(2.0);
  55. c1.doThings();
  56. c2.doThings();
  57. c3.doThings();
  58. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
B
0
C
1
B
2
C
2