mardi 24 juillet 2018

Why gcc warns about narrowing conversion only for uniform initialization?

I am trying to convert long type variable to int type variable with uniform initialization and without it. But I get compiler warning only with uniform initialization. Why is that? Why gcc does not warn in both cases? I have tried with clang also and got similar results.

This is the code:

#include <iostream>

int main() {
  long l = 1;
  int i1 = l;
  int i2 = { l };

  std::cout << i1 << std::endl;
  std::cout << i2 << std::endl;

  return 0;
}

And the only one warning I get:

$ g++ -Wall -Wextra 1.cpp
1.cpp: In function ‘int main()’:
1.cpp:6:16: warning: narrowing conversion of ‘l’ from ‘long int’ to ‘int’ inside { } [-Wnarrowing]
   int i2 = { l };

Aucun commentaire:

Enregistrer un commentaire