Why do most implementations still have cmath functions in the global namespace in C ++ 03?

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 
+7
c ++ namespaces
source share
2 answers

As the discussion in the gcc bugzilla indicated in the corresponding release, this approach will not allow the inclusion of the C header after C ++ due to the inclusion of defenders:

 #include <cmath> #include <math.h> // include skipped by include guards ... sin(x); // error: no sin in global namespace 

As you mentioned, the standard requirements for the wrapper C-library were changed after the problem was published in the defect report and the previous requirements were declared impractical from the point of view of implementation.

+2
source share

They do not do this because it does not work. Give it a try. There is no function in the C library named __C_LANGUAGE_MATH::acos .

-one
source share

All Articles