C ++ - cmath vs math.h(以及类似的c-prefix和.h扩展头) [英] C++ - cmath vs math.h (And similar c-prefixed vs .h extension headers)

查看:131
本文介绍了C ++ - cmath vs math.h(以及类似的c-prefix和.h扩展头)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些有关 iostream vs iostream.h 之间差异的信息。从我从那些收集的,他们之间的区别是,没有 .h 扩展的版本不会填充命名空间,而带有扩展名的版本。



cmath vs math.h 这是否相同?为什么 cmath (以及许多其他文件)前缀为 c ,而不是 math

解决方案


我已经看过一些关于iostream和iostream.h之间的差异的信息。


[iostream.h]不是标准标题。



它不是您提出的问题的示例。



[cmath]定义 std 命名空间中的符号,也可以在全局命名空间中定义符号。 [math.h]在全局命名空间中定义符号,也可以在 std 命名空间中定义符号。如果你包括前者并使用一个非限定符号,它可以编译一个编译器,但不能与另一个。因此,使用[math.h]是个好主意。一般来说,对于这样的头对,使用[.h]版本。



c ++ 98提供了c xxx 头不污染全局命名空间。也许这就是为什么他们被定义。然而,这是有点难以实现比污染,所以在实践中没有标准库实现我知道遵循这方面的标准,因此它终于改变,以反映c ++ 11中的现实。


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.

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?

Thank you for your time!

解决方案

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

[iostream.h] is not a standard header.

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.

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 ++ - cmath vs math.h(以及类似的c-prefix和.h扩展头)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆