顾问Stroustrup书“C++ 方案拟订语言4” 因此,我无法理解,为什么汇编者在使用旧的“=”分配方法开始变数时进行必要类型的转换:
int i1 = 7.2; // i1 becomes 7 due truncation
bool b1 = 7; // 7!=0, so b1 becomes true
但会给以下人员留下错误:
int i2 {7.2}; // error: floating-point to integer conversion
bool b2 {7}; // error: narrowing
Note, the question is not about truncation or narrowing, but what does the language/compiler do differently that one way it "can make it work" but the other don t.
编辑这一文件将产生以下成果:
error: narrowing conversion of ‘7’ from ‘int’ to ‘bool’ [-Wnarrowing]
24 | bool b2{7};