G++ 4.5 Bug: No diagnostic for narrowing in initializer list
I tried the following code:
int main()
{
int x {23.22};
}
which includes an initialization that requires narrowing, but the code compiles fine without any error or warning. On the other hand, the following code gives error:
int main()
{
int x[]{23.22};
}
Have I found a bug or what?
PS: I'm开发者_如何学Go currently using GCC 4.5.0
Looks like a bug. The following is straight out from the draft n3092:
8.5.4 List-initialization
— Otherwise, if the initializer list has a single element, the object is initialized from that element; if a narrowing conversion (see below) is required to convert the element to T, the program is ill-formed.
int x1 {2}; // OK
int x2 {2.0}; // error: narrowing
You can take a look at GCC's C++0X compliance here. The status of Initializer Lists (N2672) is 'Yes' -- but note that this is merely experimental (and hence you can expect bugs).
Update from bug report: GCC does emit a warning with the -Wconversion
flag (and no this is not covered by -Wall
).
As C++0x support is still being implemented, even if there should be an error or warning according to the Standard and there isn't, that doesn't make it a bug necessarily, just yet to be implemented. This may also occur if the draft Standard has been changed since that particular feature was implemented.
The fact of working with work-in-progress software or standards is that things which are supposed to exist according to the latest spec don't.
精彩评论