1

If a std::array is defined I need to write something like this

std::array<int,3> my_array {1,5,3};

In my option this violates the DRY principle (Don't repeat yourself), since I need to specify the length of the array explicitly and it need to corresponds to the length of the initializer list.

Is there a way to write something like

std::array<int> my_array {1,5,3};

and the constant length of the array is deduced form the length of the initializer list?

schorsch312
  • 5,553
  • 5
  • 28
  • 57
  • 5
    Use C++17 and deduction guides. http://en.cppreference.com/w/cpp/container/array/deduction_guides – underscore_d Apr 26 '18 at 14:48
  • Agreed. While the referenced duplicate shows ways that you *can* do it in c++11, I'd recommend that you don't bother, as it is fixed properly in c++17. – Gem Taylor Apr 26 '18 at 16:08

0 Answers0