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
.
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 intostd::
. Similarly,#include <cmath>
puts all the names intostd::
, 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.Gather all function declarations from math.h into
namespace neveruse
, and sayusing namespace neveruse
. Now all references to unqualifiedsin
will be ambiguous.