gpt4 book ai didi

c++ - 错误 83 错误 C2398 : conversion from 'double' to 'float' requires a narrowing conversion

转载 作者:可可西里 更新时间:2023-11-01 16:30:53 40 4
gpt4 key购买 nike

我找到了很多关于这个错误的帖子,但我可以找到克服它的方法。这是触发错误的代码:

void main(){
float f{1.3};
}

为什么在初始化列表中没有像其他变量那样发生转换?例如,这工作顺利:

float f = 1.3;

最佳答案

您评论说使用 1.3 会导致您的编译器出错。这意味着您发现了一个编译器错误。标准很清楚这不是缩小转换,因此应该允许。

引用 N4140(大致为 C++14):

8.5.4 List-initialization [dcl.init.list]

7 A narrowing conversion is an implicit conversion

[...]

-- (7.2) from long double to double or float, or from double to float, except where the source is a constant expression and the actual value after conversion is within the range of values that can be represented (even if it cannot be represented exactly), or

[...]

您的 1.3 是一个在 float 范围内的常量表达式。

我建议向 Microsoft 报告此问题,前提是这还不是已知问题。不幸的是,仅仅升级您的 Visual Studio 并不能解决这个问题。我可以在 VS2015 中重现该问题。

关于c++ - 错误 83 错误 C2398 : conversion from 'double' to 'float' requires a narrowing conversion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33605632/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com