intmain(){ int a = 1.2; // ok int b = {1.2}; // error
float c = 1e70; // ok float d = {1e70}; // error
float e = (unsignedlonglong)-1; // ok float f = {(unsignedlonglong)-1}; // error float g = (unsignedlonglong)1; // ok float h = {(unsignedlonglong)1}; // ok
constint i = 1000; constint j = 2; char k = i; // ok char l = {i}; // error
char m = j; // ok char m = {j}; // ok,因为是const类型,这里如果去掉const属性,也会报错 }
打印如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
test.cc:24:17: error: narrowing conversion of ‘1.2e+0’ from ‘double’ to ‘int’ inside { } [-Wnarrowing] int b = {1.2}; ^ test.cc:27:20: error: narrowing conversion of ‘1.0000000000000001e+70’ from ‘double’ to ‘float’ inside { } [-Wnarrowing] float d = {1e70};
test.cc:30:38: error: narrowing conversion of ‘18446744073709551615’ from ‘longlongunsignedint’ to ‘float’ inside { } [-Wnarrowing] float f = {(unsignedlonglong)-1}; ^ test.cc:36:14: warning: overflow in implicit constant conversion [-Woverflow] char k = i; ^ test.cc:37:16: error: narrowing conversion of ‘1000’ from ‘int’ to ‘char’ inside { } [-Wnarrowing] char l = {i};