Narrowing Conversion required while list initialization

旧巷老猫 提交于 2019-12-10 20:39:41

问题


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?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/44031644/narrowing-conversion-required-while-list-initialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!