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,448
  • 3
  • 21
  • 36

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
  • 203,559
  • 14
  • 181
  • 302
-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
  • 223,364
  • 34
  • 402
  • 480
Dory Zidon
  • 10,497
  • 2
  • 25
  • 39