c++ – Compiler does not support extended initialization

Question:

There is a code like this:

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

I read that the new version of C ++ 11 has support for prohibiting narrowing conversions during initialization. But the compiler does not give an error, although it should (because I am trying to convert double to int using the syntax of the new version of initialization (using curly braces), which just prohibits such unsafe conversions.

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

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

Answer:

The C ++ standard does not oblige the implementation of the language to issue a "compilation error" for cases when the standard says that the code is erroneous. (However, this should not break SFINAE, otherwise this is already a compiler bug).

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

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

However, this is enough, since it is recommended to compile the 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.

Scroll to Top