列表初始化时需要缩小转换 [英] Narrowing Conversion required while list initialization

查看:121
本文介绍了列表初始化时需要缩小转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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?

推荐答案

因为要求编译器发布诊断(针对您的情况,错误),以缩小//en.cppreference.com/w/cpp/language/list_initialization"rel =" nofollow noreferrer> 列表初始化 (又称统一初始化),从C ++ 11开始.对于没有花括号的C ++ 11之前的版本,不需要诊断.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆