Consider the following program:
#include <iostream>
#include <cmath>
int main() {
const int M_PI = 22/7;
std::cout << M_PI << '\n';
}
(Yes, I know that the value of 22/7 is 3, not a good approximation of pi.)
Both g++ 8.2.0 and clang++ 7.0.0 fail to compile this, because M_PI is
defined as a macro in <cmath>:
$ g++ -std=c++17 -pedantic-errors -c c.cpp
In file included from /o/apps/gcc-8.2.0/include/c++/8.2.0/cmath:45,
from c.cpp:2:
c.cpp: In function ‘int main()’:
c.cpp:4:15: error: expected unqualified-id before numeric constant
const int M_PI = 22/7;
^~~~
The library implementation is libc6-dev:amd64 2.27-3ubuntu1.
M_PI is defined by POSIX, not by either ISO C or ISO C++. It's not
a reserved identifier in either language, so it should be available
as a user-defined identifier in conforming C++ code.
Am I correct in thinking that this is a bug in the implementation,
or is there some C++ rule that allows conforming implementations
to reject it?
(For a similar C program, using "-std=c11" causes it to compiler
correctly.)
--
Keith Thompson (The_Other_Keith)
k...@mib.org <
http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"