There is such code:

int main() { double d {4.5}; int x {d}; } 

I read that in the new version of C ++ 11 there is support for the prohibition of narrowing conversions during initialization. But the compiler does not produce errors, although it should (for I try to convert double to int with the syntax of the new version of initialization (using curly brackets), which just prohibits such unsafe conversions.

IDE - DevC ++ 5.11 Compiler - TDM-GCC 4.9.2.

Question: So why there is no support for this new initialization? The compiler is new, it was recently downloaded with the environment.

  • one
    Gives a warning . With the appropriate keys will give an error. Actually, that's the same code with the error . -pedantic-errors added. - αλεχολυτ

1 answer 1

Standard C ++ does not oblige the implementation of the language to produce exactly the "compilation error" for cases when the standard says that the code is wrong. (However, this should not break SFINAE, otherwise it is a compiler bug).

The g ++ compiler in some cases is limited to a warning:

 double d = 4.5; int x{d}; // предупреждение в g++ int y{4.5}; // ошибка в g++ 

But this is enough, because compiling code with -Wall -pedantic -Werror .

As for the standard, int x{d}; This is undoubtedly the wrong code. There is a similar example in the [dcl.init.list] section.