cmath 与 math.h(以及类似的 c 前缀与 .h 扩展标头)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10694255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cmath vs math.h (And similar c-prefixed vs .h extension headers)
提问by golmschenk
I've seen some information about differences between things like iostream
vs iostream.h
. From what I gathered from those the difference between them is that the version without the .h
extension will not populate the namespace while the version with the extension will.
我已经看到了一些关于诸如iostream
vs 之类的东西之间差异的信息iostream.h
。从我收集的信息来看,它们之间的区别在于没有.h
扩展名的版本不会填充命名空间,而带有扩展名的版本会。
Is this the same for cmath
vs math.h
? Why is cmath
(and many other files like it) prefixed with a c
instead of just being math
? Are there more differences between them?
这对cmath
vs是一样的math.h
吗?为什么cmath
(以及许多其他类似的文件)以 ac
为前缀,而不仅仅是math
?它们之间还有更多区别吗?
采纳答案by Cheers and hth. - Alf
I've seen some information about differences between things like iostream vs iostream.h.
我已经看到了一些关于 iostream 与 iostream.h 之类的东西之间差异的信息。
[iostream.h] is not a standard header.
[iostream.h] 不是标准头文件。
it is not an example of the issue you're raising.
这不是你提出的问题的一个例子。
[cmath] defines symbols in the std
namespace, and may also define symbols in the global namespace. [math.h] defines symbols in the global namespace, and may also define symbols in the std
namespace. if you include the former and use an unqualified symbol, it may compile with one compiler but not with another. therefore it's a good idea to use [math.h]. and in general, for such header pairs, to use the [.h] version.
[cmath] 在std
命名空间中定义符号,也可能在全局命名空间中定义符号。[math.h] 在全局命名空间中定义符号,也可以在std
命名空间中定义符号。如果包含前者并使用非限定符号,则它可能会用一个编译器编译,但不能用另一个编译器编译。因此使用 [math.h] 是个好主意。通常,对于此类标头对,使用 [.h] 版本。
c++98 provided a formal guarantee of the cxxx
header not polluting the global namespace. maybe that was why they were defined. however, that was a bit harder to implement than polluting ones, so in practice no standard library implementation that i know of followed the standard in this respect, and so it was finally changed to reflect reality in c++11.
c++98 提供了 cxxx
头文件不污染全局命名空间的正式保证。也许这就是他们被定义的原因。然而,这比污染的更难实现,所以在实践中,我所知道的没有标准库实现在这方面遵循标准,所以它最终被改变以反映 c++11 中的现实。
回答by Hani Shams
Maybe this would be helpful :
也许这会有所帮助:
The C++ library includes the same definitions as the C language library organized in the same structure of header files, with the following differences:
1 - Each header file has the same name as the C language version but with a "c" prefix and no extension. For example, the C++ equivalent for the C language header file < stdlib.h > is < cstdlib>.
2 - Every element of the library is defined within the std namespace.
C++库包含与C语言库相同的头文件结构组织的相同定义,区别如下:
1 - 每个头文件的名称与 C 语言版本相同,但带有“c”前缀且没有扩展名。例如,C 语言头文件 <stdlib.h> 的 C++ 等效项是 <cstdlib>。
2 - 库的每个元素都在 std 命名空间中定义。
回答by Keith Thompson
The headers whose names start with c
are derived from the headers of the C standard library. The corresponding headers with the c
prefix removed and a .h
suffix added are identical (or very nearly identical) to the C standard library headers.
名称以 开头c
的头文件源自 C 标准库的头文件。c
删除前缀并.h
添加后缀的相应头文件与C 标准库头文件相同(或几乎相同)。
<cmath>
defines the relevant symbols under the std
namespace; <math.h>
defines them globally.
<cmath>
定义std
命名空间下的相关符号;<math.h>
全局定义它们。
(I just learned it's not quite that simple; see Alf's answer.)
(我刚刚了解到它并不那么简单;请参阅 Alf 的回答。)