我尝试了以下代码:
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/