mardi 28 mars 2017

Narrowing conversion from `int` (constant expression) to `unsigned int` - MSVC vs gcc vs clang

constexpr int i = 100;
struct F { F(unsigned int){} };
int main() { F{i}; }

The snippet above:

  • Compiles with no warnings on g++ 7 with -Wall -Wextra -Wpedantic.

  • Compiles with no warnings on clang++ 4 with -Wall -Wextra -Wpedantic.

  • Fails to compile on MSVC 2017:

    conversion from 'const int' to 'unsigned int' requires a narrowing conversion

Q: is MSVC wrong here?

live example on godbolt.org


int i = 100;
struct F { F(unsigned int){} };
int main() { F{i}; }

  • Compiles with warnings on g++ 7 with -Wall -Wextra -Wpedantic:

    narrowing conversion of 'i' from 'int' to 'unsigned int'

  • Fails to compile clang++ 4 with -Wall -Wextra -Wpedantic:

    non-constant-expression cannot be narrowed from type 'int' to 'unsigned int' in initializer list

  • Fails to compile on MSVC 2017:

    conversion from 'const int' to 'unsigned int' requires a narrowing conversion

Q: is g++ wrong here? (i.e. should it produce an hard error?)

live example on godbolt.org

Aucun commentaire:

Enregistrer un commentaire