As I understand it, in C ++ 03 #include <cmath> should declare functions only in namespace std . Starting with C ++ 11, they can be optionally declared in the global namespace. This is the result of practice when most C ++ implementations declared functions in the global namespace (presumably #include ing <math.h> ), and then simply using ::acos; etc. In namespace std .
But it seems to me that it would be just as easy for implementations to do something similar in their <cmath> :
namespace __C_LANGUAGE_MATH_H { #include <math.h> } // ... namespace std { // ... using __C_LANGUAGE_MATH_H::acos; // ... }
Why is this not practiced, but simply pollutes the global namespace? Does my proposed solution have some significant flaws that made the C ++ committee allow pollution of the global namespace in C ++ 11?
NOTE. This works, and the linker does not give any errors, at least with GCC + Binutils-ld. I really tried and edited the GCC cmath file, for example, the following, and compiled my project, which actively uses the cmath functions successfully (after fixing some calls that didnโt specify std:: in the project):
mv /usr/include/c++/5.3.0/cmath{,.bak} sed -i -e ' s@ \(# *include <math.h>\)@namespace __C_LANGUAGE_MATH_H\n{\n\1\n}@' \ -e ' s@ \(using \+\)::@\1__C_LANGUAGE_MATH_H::@' /usr/include/c++/5.3.0/cmath
c ++ namespaces
Ruslan
source share