忽略 C++0x 中的缩小转换会产生什么后果
自从在 g++ 中切换 C++0x 标准以来,我开始看到“缩小转换”错误,特别是在从“int”转换为“short”时,尽管我知道该错误涵盖了更广泛的 转化。
任何人都可以阐明引入这种额外安全级别的理由吗?禁用此错误可能会产生什么后果? (除了潜在的精度损失)。
谢谢。
Since switching on the C++0x standard in g++, I've started seeing 'narrowing conversion' errors, particularly when converting from an 'int' to a 'short' although I understand the error covers a much broader swath of conversions.
Can anyone shed some light on the rational for introducing this extra level of safety?, What are the possible consequences of disabling this error? (apart from the potential loss of precision).
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自赋值和复合赋值运算符[expr.ass]
以及来自列表初始化[dcl.ini.list]
所以基本上你不能忽略它,你的程序在存在缩小转换的情况下格式不正确。
来自实施合规性:
Bjarne Stroustroup 说这个:
因此在某种程度上,缩小也提高了类型安全性。
From Assignment and compound assignment operators [expr.ass]
and from List-initialization [dcl.ini.list]
So basically you can't ignore it, your program is ill-formed in presence of narrowing conversions.
From Implementation compliance:
Bjarne Stroustroup say this:
So in a way, narrowing also increases type safety.