Is it possible to pass the exact braced-init-list to std::array
’s constructor? This might be necessary since std::array
does not support initializer list assignment.
Trying to adapt the accepted answer of this question: How to construct std::array object with initializer list? (answer’s shortened link: https://stackoverflow.com/a/6894191/4385532 ) to my problem, I came up with the following “solution” to the problem of passing a braced-init-list to the constructor of std::array
:
template<class T, std::size_t N> struct ArrayWrapper
{
std::array<T,N> arr;
ArrayWrapper(T &&... init) : arr{{std::forward<T>(init)...}} {}
}
I was hoping that this would allow the following syntax:
int main()
{
ArrayWrapper<int, 4> aw{{1,2,3,4}};
std::cout << aw.arr[2] << '\n';
}
However, I failed. This won’t compile: http://ideone.com/VJVU1X
I’m not sure what is my error. I hoped that this line
ArrayWrapper(T &&... init) : arr{{std::forward<T>(init)...}} {}
would catch the exact elements of the braced-init-list passed to ArrayWrapper
’s constructor, and forward them to the constructor of std::array
.
What do I fail to understand?