为什么 GCC 允许在 C++ 中使用 round()即使有 ansi 和迂腐的标志?
即使使用 -ansi
和 -pedantic
标志,这个程序也能在 GCC 下编译,有充分的理由吗?
#include <cmath>
int main (int argc, char *argv [])
{
double x = 0.5;
return static_cast<int>(round(x));
}
使用 g++ -ansi -pedantic -Wall test.cpp -o test
编译干净(甚至没有警告)。
我看到两个问题:
round()
不应该在 ISO-conformant 模式下对 C++ 可用(因为它来自 C99)- 即使
round()
在此可用在这种情况下,它应该只来自std
命名空间,
我错了吗?
Is there a good reason why this program compiles under GCC even with the -ansi
and -pedantic
flags?
#include <cmath>
int main (int argc, char *argv [])
{
double x = 0.5;
return static_cast<int>(round(x));
}
This compiles clean (no warnings, even) with g++ -ansi -pedantic -Wall test.cpp -o test
.
I see two problems:
round()
shouldn't be available to C++ in ISO-conformant mode (since it comes from C99)- Even if
round()
were available in this case, it should only be so from thestd
namespace
Am I wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个错误。它已经存在了令人惊讶的很长一段时间。显然,解决这个问题的集体意愿还不够。随着新版本的 C++ 即将采用 math.h 中的 C99 函数,它似乎不太可能被修复。
This is a bug. It's been around for a surprisingly long while. Apparently, there has not been enough of a collective desire to fix it. With a new version of C++ just around the corner which will adopt the C99 functions from math.h, it seems unlikely it will ever be fixed.
我可能在这里偏离了基础,但 gcc 的 -ansi 标志是否适用于代码构造(即禁用 GCC 语言 扩展)而不是将所有库也切换到严格的 ANSI 兼容模式?
I might be off base here but doesn't gcc's -ansi flag apply to the code constructs (ie, disable GCC language extensions) rather than switching all libraries into strict ANSI compliant mode as well?
我相信标准指定了需要定义哪些符号以及在哪个标头中定义它们。我不认为标准规定不能定义其他符号。更重要的是,
std::round()
不会由名为round()
的自由符号定义。I believe that the Standards specify what symbols are required to be defined and in which header they are defined. I do not believe that the Standards state that no other symbols may be defined. More to the point,
std::round()
will not be defined by a free symbol calledround()
can be defined.