J
Juha Nieminen
How to properly initialize a std::array<std::array<float, 2>, 2> using
an initializer list?
an initializer list?
How to properly initialize a std::array<std::array<float, 2>, 2> using
an initializer list?
Casey Carter said:According to the standard (23.3.2.1/2): An array is an aggregate (8.5.1)
that can be initialized with the syntax
array<T, N> a = { initializer-list };
The fully-braced form of initializer would then be
array<T, N> a = {{ initializer-list }};
Which, despite being non-standard
If that's the case, then neither gcc nor clang implement it properly
because both give a warnings when I try it, rather than just working
as intended.
So what would be the standard way?
Casey Carter said:Considering how that initialization style depends on dropping braces,
there's no way it can possibly work in the presence of nested arrays.
If that's the case, then neither gcc nor clang implement it properly
because both give a warnings when I try it,
Nobody said:The implementation is allowed to emit warnings about anything it likes,
including perfectly valid code.
The standard only requires the implementation to accept the above syntax;
it doesn't require it to do so quietly.
But as noted, they do not accept things like
std::array<std::array<int, 2>, 2> a = { { 1, 2 }, { 3, 4 } };
even though they should.
Nobody said:It isn't possible to specify the syntax for higher dimensions without
either specifying the implementation or changing the base language syntax.
But std::vector<std::vector<int>> v = { { 1, 2 }, { 3, 4 } }; works just
fine.
What's so different about std::array?
But std::vector<std::vector<int>> v = { { 1, 2 }, { 3, 4 } }; works just
fine. What's so different about std::array?
Nobody said:Only for C++11. Previously, you couldn't use any form of initialiser list.
Casey Carter said:If this all seems ridiculous to you, you are not alone. The standard
does some contortions to satisfy the requirement that std::array be an
aggregate, so that std::array can fit nearly all use cases for C-style
arrays.
An array is an aggregate (8.5.1) that can be initialized with the syntax
array<T, N> a = { initializer-list };
where initializer-list is a comma-separated list of up to N elements
whose types are convertible to T.
(Since std::initializer_list is not, AFAIK, a dynamic data container, is
there a technical reason why its size() function cannot be constexpr?)
"Nearly" meaning "with the exception of multidimensional arrays, which
cannot be initialized in the same way as multidimensional C-style arrays"?
I'm still not quite sure what would be the problem in std::array supporting
std::initializer_list, except perhaps for the small annoyance that there
doesn't seem to be a way to get the size of the initializer list at
compile time in order to give an error if too many elements are specified.
(Since std::initializer_list is not, AFAIK, a dynamic data container, is
there a technical reason why its size() function cannot be constexpr?)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.