Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 currently using GCC 4.5.0

like image 307
Saurabh Manchanda Avatar asked Aug 22 '10 13:08

Saurabh Manchanda


1 Answers

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).

like image 144
dirkgently Avatar answered Oct 14 '22 00:10

dirkgently