C++14 中的统一初始化双分配给浮点变量不会产生缩小错误
Uniform initialization in C++14 double assigned to float variable does not produce narrowing error
根据我对统一初始化的理解,以下代码应该会产生缩小错误,但它不会:
#include<iostream>
int main() {
double d;
float f = {d}; // should produce a narrowing error
return 0;
}
我认为这是一个错误,因为编译器必须考虑 d 的所有可能值,换句话说,d 可能包含的值的范围大于 float 可以包含的范围。
gcc version 4.9.3
这确实会使用 g++ -std=c++11
发出警告,但是如果您省略 c++11 编译器标志(使用 g++ 4.9.2)则不会。
使用 clang++ -std=c++14 -stdlib=libc++ -Wall -pedantic -Wdeprecated -Wextra
这不会编译,但会给出缩小错误而不是仅警告(使用 clang++ 3.6.0)。
需要收缩转换才能发出诊断。警告或错误满足。
运行 你的代码 here(gcc 4.9.2 是我可以 link 使用的最接近的版本)发出警告
warning: narrowing conversion of 'd' from 'double' to 'float' inside { } [-Wnarrowing]
这满足了标准的要求。
如果你真的想要一个错误,你总是可以使用 -Werror
或将所有警告视为错误或 -pedantic-errors
将缩小转换视为错误。
标准没有区分"errors"和"warnings"。您的代码是 ill-formed,但标准是这样说的:
N4140 [intro.compliance]/2.2:
If a program contains a violation of any diagnosable rule or an occurrence of a construct described in
this Standard as “conditionally-supported”when the implementation does not support that construct,
a conforming implementation shall issue at least one diagnostic message.
[intro.compliance]/8:
A conforming implementation may have extensions (including additional library functions), provided they do
not alter the behavior of any well-formed program. Implementations are required to diagnose programs that
use such extensions that are ill-formed according to this International Standard. Having done so, however,
they can compile and execute such programs.
GCC 为 ill-formed 代码发出诊断,因此它在这方面是兼容的。请注意,如果您传递 -pedantic-errors
.
,它 将 发出错误
来自此处的错误:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55783
The standard only requires that "a conforming implementation shall
issue at least one diagnostic message" so compiling the program with a
warning is allowed. As Andrew said, -Werror=narrowing allows you to
make it an error if you want.
根据我对统一初始化的理解,以下代码应该会产生缩小错误,但它不会:
#include<iostream>
int main() {
double d;
float f = {d}; // should produce a narrowing error
return 0;
}
我认为这是一个错误,因为编译器必须考虑 d 的所有可能值,换句话说,d 可能包含的值的范围大于 float 可以包含的范围。
gcc version 4.9.3
这确实会使用 g++ -std=c++11
发出警告,但是如果您省略 c++11 编译器标志(使用 g++ 4.9.2)则不会。
使用 clang++ -std=c++14 -stdlib=libc++ -Wall -pedantic -Wdeprecated -Wextra
这不会编译,但会给出缩小错误而不是仅警告(使用 clang++ 3.6.0)。
需要收缩转换才能发出诊断。警告或错误满足。
运行 你的代码 here(gcc 4.9.2 是我可以 link 使用的最接近的版本)发出警告
warning: narrowing conversion of 'd' from 'double' to 'float' inside { } [-Wnarrowing]
这满足了标准的要求。
如果你真的想要一个错误,你总是可以使用 -Werror
或将所有警告视为错误或 -pedantic-errors
将缩小转换视为错误。
标准没有区分"errors"和"warnings"。您的代码是 ill-formed,但标准是这样说的:
N4140 [intro.compliance]/2.2:
If a program contains a violation of any diagnosable rule or an occurrence of a construct described in this Standard as “conditionally-supported”when the implementation does not support that construct, a conforming implementation shall issue at least one diagnostic message.
[intro.compliance]/8:
A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any well-formed program. Implementations are required to diagnose programs that use such extensions that are ill-formed according to this International Standard. Having done so, however, they can compile and execute such programs.
GCC 为 ill-formed 代码发出诊断,因此它在这方面是兼容的。请注意,如果您传递 -pedantic-errors
.
来自此处的错误:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55783
The standard only requires that "a conforming implementation shall issue at least one diagnostic message" so compiling the program with a warning is allowed. As Andrew said, -Werror=narrowing allows you to make it an error if you want.