Question

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?

Was it helpful?

Solution

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)... }};
}

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top