Re: Why no conversion from std::initializer_list to array?
Am 21.04.2012 03:45, schrieb Ivan Godard:
This doesn't work:
std::initializer_list<int> i = {2,3};
int arr[2] = i;
Note that arrays are not copyable, therefore this assignment couldn't
work even if std::initializer_list would provide an implicit
conversion to arrays (This conversion function would need to return a
reference, though and this leads to some further complications as
described below).
Assuming you meant something like
const int (&arr)[2] = i;
or your question was meant as an alternative way to provide an
"element-by-element" initializer for an array in terms of an
initializer list, this would require that:
a) The initializing std::initializer_object is a constant expression,
like declared by
constexpr std::initializer_list<int> i = {2,3};
b) Instances of std::initializer_list are literal types that provide a
constexpr access to size().
There are good reasons for (b), there also exists a corresponding
proposal for such an extension, see
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3305.html
Why didn't the language include this with the rest of the uniform
initialization stuff?
This is not as simple as it might look as described above, especially
because std::initializer_list objects are just normal objects and
because they are currently not yet literal types.
There was a corresponding request to make the underlying array of
std::array available, see
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#930
which was unfortunately rejected. Personally I think that providing a
named function like c_array() would be indeed useful for std::array.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]