fork download
  1. #include <string>
  2. #include <tuple>
  3. #include <iostream>
  4.  
  5. template<typename Tuple, std::size_t... Is>
  6. void print_tuple(const Tuple& tup, std::index_sequence<Is...>);
  7.  
  8. template<typename Tuple>
  9. using tuple_indices = std::make_index_sequence<std::tuple_size<Tuple>::value>;
  10.  
  11. template<typename... Ts>
  12. class B {
  13. public:
  14. B(int i, bool b, float f, const Ts&... rest) :
  15. properties(std::make_tuple(i, b, f, rest...)) {
  16. }
  17. virtual std::string id() const = 0;
  18. std::tuple<int, bool, float, Ts...> properties;
  19. };
  20.  
  21. class A : public B<float, std::string, std::string> {
  22. using B::B;
  23. virtual std::string id() const {
  24. return "Class A";
  25. }
  26. };
  27.  
  28. class C : public B<std::string> {
  29. using B::B;
  30. virtual std::string id() const {
  31. return "Class C";
  32. }
  33. };
  34.  
  35. template<typename... Ts>
  36. void test(const B<Ts...>& base) {
  37. std::cout << base.id() << '(';
  38. print_tuple(base.properties, tuple_indices<decltype(base.properties)>{});
  39. std::cout << ')' << std::endl;
  40. }
  41.  
  42. int main() {
  43. A foo(12, true, 3.14, 6.28, "foo", "bar");
  44. C baz(36, false, .33, "baz");
  45.  
  46. test(foo);
  47. test(baz);
  48. }
  49.  
  50. template<typename Tuple, std::size_t... Is>
  51. void print_tuple(const Tuple& tup, std::index_sequence<Is...>) {
  52. using dummy = int[];
  53. static_cast<void>(dummy {
  54. 0, (static_cast<void>(std::cout << (Is ? ", " : "") << std::get<Is>(tup)), 0)...
  55. });
  56. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
Class A(12, 1, 3.14, 6.28, foo, bar)
Class C(36, 0, 0.33, baz)