0

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 ?

4
  • 2
    Perhaps you're looking for a constructor taking std::initializer_list? Commented Mar 4, 2015 at 1:58
  • What is your question? The last snippet should compile, combine that with std::forward and you have your original desired constructor. Commented Mar 4, 2015 at 2:24
  • The last part is me answering my own question Commented Mar 4, 2015 at 2:25
  • @Amxx Then you should post it as an answer. The reason you need to use braces instead of parentheses is because std::array is an aggregate and has no user defined constructors, so you must perform aggregate initialization. Commented Mar 4, 2015 at 2:36

1 Answer 1

3

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...}}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.