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

爱⌒轻易说出口 提交于 2019-12-07 11:17:35

问题


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.



来源:https://stackoverflow.com/questions/19159541/disable-math-h-crap-when-working-with-cmath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!