fork download
  1. #include <iostream>
  2. #include <array>
  3. #include <tuple>
  4.  
  5. class Car {
  6. public:
  7. Car(const std::string& color, int age): color_(color), age_(age) {}
  8. // ...
  9. //private:
  10. std::string color_;
  11. int age_;
  12. };
  13.  
  14. template <typename CarType, typename... Args ,size_t... Is>
  15. std::array<CarType,sizeof...(Is)> make_cars(std::index_sequence<Is...>,Args&&... args )
  16. {
  17. return { (Is,CarType(args...))... };
  18. }
  19.  
  20. class ThreeIdenticalCars {
  21. //private:
  22. public:
  23. std::array<Car, 3> list;
  24. //public:
  25. ThreeIdenticalCars(const std::string& color, int age) :
  26. list(make_cars<decltype(list)::value_type>(
  27. std::make_index_sequence<std::tuple_size<decltype(list)>::value>(),
  28. color,
  29. age
  30. ))
  31. {}
  32. };
  33.  
  34. int main()
  35. {
  36. ThreeIdenticalCars threecars("red", 10);
  37.  
  38. for(auto& car : threecars.list)
  39. std::cout << car.color_ << " " << car.age_ << std::endl;
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 4384KB
stdin
Standard input is empty
stdout
red 10
red 10
red 10