Re: array + return + initializer (C++0X) - is this a bug Organization: Arcor
On 2011-04-07 10:06, Johannes Schaub wrote:
Helmut Jarausch wrote:
Hi,
I'm not sure if the following error message identifies a bug
in gcc-4.6 / gcc-4.6-git
#include<array>
using std::array;
/*
In function 'std::array<int, 3ul> RWTH_UTM()':
error: could not convert '{32, 293945, 5629196}' to 'std::array<int, 3ul>'
*/
array<int,3> RWTH_UTM() {
return {32,293945,5629196};
}
array<int,3> Standort {32,293945,5629196}; // OK
No, this is according to the spec, and arguably the second one is invalid
too. You are using brace-elision here: The aggregational structure of
"array<int, 3>" is "{<array int[3]> }". The array in fully braced syntax
needs its own braces. So you need two braces: One for the struct, and one
for the array. You can elide them only in declarations "of the form T x = {
a };", with an explicit footnote that this elision is not allowed "in other
uses of list-initialization".
You make a very good point here, which I did not properly honor in my suggestion for a possible make_array implementation. This restriction has the unfortunate effect, that the initialization of std::array is underspecified especially for the zero-length container. According to [array.overview] p. 2 we have:
"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."
This has the unfortunate consequences, that it does not say anything about other feasible initialization forms, which influences especially the zero-length array case. It is unclear whether
std::array<int, 0> b1{};
is well-formed or whether
std::array<int, 0> b2{{}};
can or must be used instead, which obviously depends on the actual implementation. The latter form would be invalid, if the specialization would be an empty class, the first form would be invalid, if the specialization contains a single element of type T[1]. I will consider this in my next update for make_array, which will use a parenthesized form of value-initialization for the zero-length case.
As a side-aspect of this it is not clear whether a zero-length array of a non-DefaultConstructible type satisfies the DefaultConstructible requirements. Consider:
struct NoDef { NoDef() = delete; };
std::array<NoDef, 0> ad; // OK?
Personally I would appreciate if this initialization would be required to be well-formed. I'm aware that this would invalidate some currently existing implementations of std::array, but IMO a zero-length std::array should be required to be an empty class anyway.
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! ]