Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 245
Dan Moulding Avatar asked Dec 10 '09 17:12

Dan Moulding


1 Answers

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.

like image 121
Dan Moulding Avatar answered Oct 06 '22 02:10

Dan Moulding