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

标签 c++ c++11 casting floating-point compiler-bug

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

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/

相关文章:

c++ - C++11 中的数组声明和初始化

visual-c++ - CWnd 到 CComboBox 的 dynamic_cast 在 OnSize 函数中产生一个空指针

elasticsearch - 如何将脚本化度量标准中的Elasticsearch字段的类型从Long更改为Double?

c++ - 如何更改 C++ 引用所引用的变量?

C++:为用户定义的类型进行转换

c++ - Clang 3.1 和用户定义的文字

c++ - 转换一个值为 255 的 char 会导致 unsigned int 值为 4294967295,这是什么原因?

c++ - 需要有关 getline 函数错误 : no matching function for call to 'getline(std::istream&, char&)' | 的编译错误的帮助

java - 使用 CORBA 连接客户端(在 VirtualBox 上)和服务器(在本地主机上)- org.omg.CORBA.BAD_PARAM :

c++ - 无法分配给 const 成员函数中的非静态数据成员