Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable math.h crap when working with cmath [duplicate]

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.

like image 465
The Quantum Physicist Avatar asked Dec 06 '22 05:12

The Quantum Physicist


2 Answers

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.

like image 158
Pete Becker Avatar answered Jan 31 '23 20:01

Pete Becker


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

like image 30
n. 1.8e9-where's-my-share m. Avatar answered Jan 31 '23 19:01

n. 1.8e9-where's-my-share m.