cmath头混乱 [英] cmath header confusion
问题描述
数学函数的命名空间是什么?全球或std?
What is the namespace for math functions? Global or std?
请考虑 cos 函数。它有3个重载。但也有来自math.h的遗产 cos
。 C不知道有关函数重载的任何内容。因此, cos(x)
无法解析为 cos(float)
。解决方案是显式调用单精度版本 cosf(x)
。我错过了什么?
Consider cos function. It has 3 overloads. But there is also legacy cos
from math.h. C doesn't know anything about function overloading. Therefore cos(x)
can't be resolved to cos(float)
. The solution is to call the single precision version explicitly cosf(x)
. Did I miss anything?
谢谢。
推荐答案
通过在C ++中包括< math.c>
和< cmath>
命名空间。 I.E.包括< math.h>
也会让你重载。
You get the same functions by including <math.c>
and <cmath>
in C++, the only differences is the namespace. I.E. including <math.h>
also gives you the overload.
理论上,使用< math.h>
为您提供在全局命名空间和std命名空间中定义的符号,同时使用< cmath>
给你在std命名空间中定义的符号,而不是在全局命名空间中。
In theory, in C++03, using <math.h>
gives you the symbols defined in the global namespace and also in the std namespace while using <cmath>
gives you the symbols defined in the std namespace and not in the global namespace.
实践是不同的,C ++ 0X将理论与实践对齐。 < math.h>
为您提供在全局命名空间中定义的符号,也可以在std命名空间中使用< cmath>
给你在std命名空间中定义的符号,也可能在全局命名空间中。
The practice is different and C++ 0X aligned the theory with the practice. <math.h>
gives you the symbols defined in the global namespace and perhaps also in the std namespace while using <cmath>
gives you the symbols defined in the std namespace and perhaps also in the global namespace.
这篇关于cmath头混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!