c++ - G++ 4.5 错误 : No diagnostic for narrowing in initializer list

标签 c++ g++ c++11 narrowing

我尝试了以下代码:

int main()
{
   int x {23.22};
}

其中包括需要缩小的初始化,但代码编译正常,没有任何错误或警告。另一方面,以下代码给出了错误:

int main()
{
   int x[]{23.22};
}

我是发现了错误还是什么?

PS:我目前使用的是 GCC 4.5.0

最佳答案

看起来像一个错误。以下直接来自 n3092 草案:

8.5.4 List-initialization

— Otherwise, if the initializer list has a single element, the object is initialized from that element; if a narrowing conversion (see below) is required to convert the element to T, the program is ill-formed.

int x1 {2}; // OK
int x2 {2.0}; // error: narrowing

您可以查看 GCC 的 C++0X 合规性 here. Initializer Lists ( N2672 ) 的状态为"is"——但请注意,这只是实验性的(因此您可能会遇到错误)。

更新自 bug report : GCC 确实会发出带有 -Wconversion 标志的警告(-Wall 不包含此内容)。

关于c++ - G++ 4.5 错误 : No diagnostic for narrowing in initializer list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3541661/

相关文章:

c++ - 插入()或制作新的

c++ - 传递参数的 g++ 问题

c++ - 我可以在预处理器和编译器之间压缩我自己的程序吗?

c++ - 在 C++11 中创建指针 vector 的惯用方法?

C++11 正则表达式子匹配

c++ - boost::intrusive 中的线程安全保证

c++编程问题

c++ - 如果函数调用是 return 语句,编译器能否自动 move 函数参数?

gcc - 如何在不使用 Unix 中的 ./a.out 命令的情况下执行 a.out 文件

c++ - "Capture"lambda 函数中的变量解析为参数