列表初始化时需要缩小转换 [英] Narrowing Conversion required while list initialization
问题描述
我在cpp参考网站上阅读了有关缩小转换的信息.我有点理解,但是我没有得到的是为什么错误仅出现在第一行.
I read about narrowing conversion on the cpp reference website. I kind of understood it but what i am not getting is that why is the error present only in the first line.
long double ld = 3.1415926536;
int a{ld}, b = {ld}; // error: narrowing conversion required
int c(ld), d = ld; // ok: but value will be truncated
为什么错误仅出现在第一行而不是第二行?
Why is the error only present in first line and not the second?
推荐答案
因为要求编译器发布诊断(针对您的情况,错误),以缩小
Because the compiler is required to issue a diagnostic (in your case error) for narrowing only for list initialization (a.k.a. uniform initialization), introduced starting with C++11. For the pre-C++11 initialization without curly braces, there is no diagnostic required.
有关更多详细信息,请参见 cppreference.com 文档.
See the cppreference.com documentation for more details.
有关为何仅要求编译器发出警告而不一定是错误的信息,另请参见此答案.
Also see this answer as to why the compiler is only required to issue a warning, not necessarily an error.
这篇关于列表初始化时需要缩小转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!