0

In C you can do int a[] = {1,2,3,4,5}, but C++11 std::array<int> a = {1,2,3,4,5} will give a "too few template parameters" compile error. Any way around this?

jcai
  • 3,346
  • 3
  • 19
  • 33

2 Answers2

5

The best you can have is a make_array, something like:

template<typename T, typename...Ts>
constexpr std::array<T, 1 + sizeof...(Ts)> make_array(T&& head, Ts&&...tail)
{
     return {{ std::forward<T>(head), std::forward<Ts>(tail)... }};
}
Jarod42
  • 190,553
  • 13
  • 166
  • 271
-3

Implementation of std::array:

template<typename T, std::size_t N>
struct array {
    T array_impl[N];
};

So this Should work:

std::array<std::int, 5> a = {{ 1, 2, 3, 4, 5 }};

which is essentially just like (as the compiler agreed to drop off the inner curly brackets.

std::array<std::int, 5> a = { 1, 2, 3, 4, 5 };

See

juanchopanza
  • 216,937
  • 30
  • 383
  • 461
Dory Zidon
  • 10,132
  • 2
  • 23
  • 37