Consider the following piece of code:
struct foo {
std::vector<int> v;
foo(std::initializer_list<int> L) : v{L} {}
};
The code above compiles fine and initializes v
as expected. Now consider the following piece of code:
struct bar {
std::array<int, 3> a;
bar(std::initializer_list<int> L) : a{L} {}
};
The above piece of code gives a compile error.
error: no viable conversion from 'std::initializer_list' to 'int'
Searching the web I found that the "proper" way to initialize a member std::array
with a std::list_initializer
is to use reinterpret_cast
in the following manner:
bar(std::initializer_list<int> L) : a(reinterpret_cast<std::array<int, 3> const&>(*(L.begin()))) {}
Q:
Why I can initialize a member std::vector
with an std::initializer_list
in the initialization list of a constructor but I can't a member std::array
?
Is the work-around showed above with reinterpret_cast
the proper way to initialize a member std::array
with a std::initializer_list
?