加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 列表初始化的缩小转换是错误还是警告?

发布时间:2020-12-16 07:15:00 所属栏目:百科 来源:网络整理
导读:参见英文答案 Why doesn’t narrowing conversion used with curly-brace-delimited initializer cause an error?????????????????????????????????????2个 目前我是自学C入门第5版.文字说: When used with variables of built-in type,this form of initia
参见英文答案 > Why doesn’t narrowing conversion used with curly-brace-delimited initializer cause an error?????????????????????????????????????2个
目前我是自学C入门第5版.文字说:

When used with variables of built-in type,this form of initialization has one
important property: The compiler will not let us list initialize variables of built-in type if
the initializer might lead to the loss of information:

这是示例代码:

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

但是当我在C shell上自己尝试时:

long double a=3.14159265354;
 int b(a);
 int c{a};
 std::cout<<a<<std::endl<<b<<std::endl<<c<<std::endl;

它只是发出警告–Wararrowing但程序成功执行.为什么?

解决方法

该标准规定,如果程序格式错误,则需要进行诊断.在支撑初始化器内发生缩小转换的情况就是这种情况.

也就是说,标准不区分错误和警告.

1.3.6诊断消息[defns.diagnostic]

message belonging to an implementation-defined subset of the implementation’s output messages

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读