cmath vs math.h(以及类似的 c 前缀 vs .h 扩展头文件)

2022-01-11 00:00:00 header include c++

我已经看到了一些关于 iostreamiostream.h 之类的差异的信息.根据我从这些收集到的信息,它们之间的区别在于没有 .h 扩展名的版本不会填充命名空间,而带有扩展名的版本会.

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.

cmathmath.h 是否相同?为什么 cmath(以及许多其他类似的文件)以 c 为前缀,而不仅仅是 math?它们之间还有更多的区别吗?

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?

推荐答案

我已经看到了一些关于 iostream 与 iostream.h 之类的差异的信息.

I've seen some information about differences between things like iostream vs iostream.h.

[iostream.h] 不是标准头文件.

[iostream.h] is not a standard header.

这不是您提出的问题的示例.

it is not an example of the issue you're raising.

[cmath] 在 std 命名空间中定义符号,也可以在全局命名空间中定义符号.[math.h] 在全局命名空间中定义符号,也可以在 std 命名空间中定义符号.如果您包含前者并使用非限定符号,则它可以用一个编译器编译,但不能用另一个编译器编译.因此使用 [math.h] 是个好主意.通常,对于此类标头对,使用 [.h] 版本.

[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.

c++98 提供了 cxxx 标头不会污染全局命名空间的正式保证.也许这就是他们被定义的原因.但是,这比污染的实现要难一些,所以在实践中,我所知道的标准库实现在这方面没有遵循标准,因此最终在 c++11 中对其进行了更改以反映现实.

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.

相关文章