-->

Disable math.h crap when working with cmath [dupli

2019-05-05 22:40发布

问题:

This question already has an answer here:

  • cmath header confusion 4 answers

I had a problem previously because of functions being overloaded without std::. And the curse is still happening every now and then because I don't use using namespace std;.

Removing using namespace std causes the program to get crap results

Is there a way to disable all those non-std functions that come from c and only work with c++ functions under the namespace std (without having to use using namespace std;)?

In other words: I want to get an error if I use sin() rather than std::sin() so that I won't do that mistake. Of c ourse, not only for sin, but every function that has a conflict with math.h.

回答1:

Unfortunately, there's no way to do that. The rule is that #include <math.h> puts all of the names into the global namespace, and is also allowed to put them into std::. Similarly, #include <cmath> puts all the names into std::, and is allowed to also put them into the global namespace. The reason for allowing the extraneous namespaces is simply that the pure versions are unimplementable in general without major surgery to existing libraries that may not even be under the control of the C++ compiler folks.



回答2:

Gather all function declarations from math.h into namespace neveruse, and say using namespace neveruse. Now all references to unqualified sin will be ambiguous.