G ++ 4.5 Error: no diagnostics for narrowing in the list of initializers

I tried the following code:

int main() { int x {23.22}; } 

which includes initialization, which requires narrowing, but the code compiles without any errors or warnings. On the other hand, the following code gives an error:

 int main() { int x[]{23.22}; } 

I found a mistake or what?

PS: I am currently using GCC 4.5.0

+6
c ++ c ++ 11 g ++
source share
2 answers

Looks like a mistake. The following directly follows from draft n3092:

8.5.4 Initializing a List

- Otherwise, if the list of initializers has one element, the object is initialized from this element; if conversion narrowing (see below) is required to convert an element to T, the program is poorly formed.

 int x1 {2}; // OK int x2 {2.0}; // error: narrowing 

You can take a look at GCC C ++ 0X compliance here. The status of the initializer lists ( N2672 ) is โ€œYesโ€ - but note that this is just experimental (and therefore you can expect errors).

Update error report : GCC issues a warning with the -Wconversion flag (and this does not apply to -Wall ).

+7
source share

Since support for C ++ 0x is still running, even if there should be an error or warning in accordance with the standard, and this is not so, this does not necessarily make a mistake, but remains to be implemented. This can also happen if the draft Standard has been changed since this feature was implemented.

The fact of working with software or standards for working in the process is that things that must exist in accordance with the latest specification do not work.

0
source share

All Articles