fork(3) download
  1. #include <array>
  2. #include <utility>
  3. #include <iostream>
  4.  
  5. const int N = 5;
  6.  
  7. struct A
  8. {
  9. int x;
  10. A(int x) : x(x) { std::cout << "Created" << std::endl; }
  11. A(const A& rhs) : x(rhs.x) { std::cout << "Copied" << std::endl; }
  12. // A(const A& rhs) = delete;
  13. };
  14.  
  15. int makeElement(size_t i)
  16. {
  17. return i * i;
  18. }
  19.  
  20. template<size_t... Inds>
  21. std::array<A, sizeof...(Inds)> makeArrayImpl(std::integer_sequence<size_t, Inds...>)
  22. {
  23. return { makeElement(Inds)... };
  24. }
  25.  
  26. std::array<A, N> makeArray()
  27. {
  28. return makeArrayImpl(std::make_index_sequence<N>{});
  29. }
  30.  
  31. int main()
  32. {
  33. const std::array<A, N> arr = makeArray();
  34.  
  35. for (const auto& item : arr)
  36. {
  37. std::cout << item.x << std::endl;
  38. }
  39. }
  40.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Created
Created
Created
Created
Created
0
1
4
9
16