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 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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With