Why does GCC allow use of round() in C++ even with the ansi and pedantic flags?

南笙酒味 提交于 2019-12-19 18:56:22

问题


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:

  1. round() shouldn't be available to C++ in ISO-conformant mode (since it comes from C99)
  2. Even if round() were available in this case, it should only be so from the std namespace

Am I wrong?


回答1:


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.




回答2:


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?




回答3:


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 called round() can be defined.



来源:https://stackoverflow.com/questions/1882689/why-does-gcc-allow-use-of-round-in-c-even-with-the-ansi-and-pedantic-flags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!