4

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.

alfC
  • 14,261
  • 4
  • 67
  • 118
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
  • Don't you get an error if you omit `math.h` and use `sin` without `std::` ? – cnicutar Oct 03 '13 at 12:44
  • 1
    Why not prepend with `std::`? – Jesse Good Oct 03 '13 at 12:47
  • 1
    @cnicutar: The problem is that `` and friends may (or may not) dump some or all of the functions into the global namespace. The "crap" in the linked question came from accidentally calling `::abs(int)` instead of `std::abs(double)`. – Mike Seymour Oct 03 '13 at 12:49
  • 1
    @JesseGood: Because sometimes we make mistakes. That's why the question is asking how to make it an error if we make that particular mistake. – Mike Seymour Oct 03 '13 at 12:50
  • @MikeSeymour Oh good to know. I thought `cmath` puts everything in `std::`. – cnicutar Oct 03 '13 at 12:51
  • @MikeSeymour: I see now, although I think typing `std::` should come as second nature. – Jesse Good Oct 03 '13 at 12:58
  • It's all about doing a mistake! a mistake could get you to debug for hours because there's no specific way to search for it but recheck the whole code! It's so nasty! – The Quantum Physicist Oct 03 '13 at 13:26
  • 1
    @TheQuantumPhysicist Agreed it is a very real problem this [question](http://stackoverflow.com/questions/19123631/float-to-double-misinterpretation-g) yesterday was due to not using the `std` version and it took a while by S) standards for people to figure it out. – Shafik Yaghmour Oct 03 '13 at 18:26

2 Answers2

8

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.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
7

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

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243