Amxx
Amxx

Reputation: 3070

variadic template for forward list initialization of std::array

I have a geometry structure describing a point in N dimensions

template <typename T, std::size_t N>
class point : public std::array<T, N>

Among the many constructors / methods I'd like to add to this class, I cannot figure out how to build a variadic constructor. I've tried

template<typename ...Args>
point(Args&&...t) : std::array<T,N>(std::forward<Args>(t)...)
{
}

but that doesn't work

std::array<int,5> p1 = {0, 1, 1, 2, 3}; // OK
point<int,5>      p2 = {0, 1, 1, 2, 3}; // ERROR

how can I define such a constructor ?

Upvotes: 0

Views: 1023

Answers (1)

Amxx
Amxx

Reputation: 3070

This behaviour comes from the specificity of std::array. Being an agregate without common constructor means you can't initialize it using std::forward or std::initializer_list

On should rather use double braces like so:

template<typename ...Args>
point(Args&&... args) : std::array<T,N>{{args...}}

Upvotes: 3

Related Questions