C++ 程序中的 #include <cmath> 与 #include <math.h>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15656434/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:36:10  来源:igfitidea点击:

#include <cmath> vs #include <math.h> in a C++ program

c++ccoding-styleincludeheader-files

提问by Violet Giraffe

What are the considerations for including the former rather than the latter in a C++ program? I always include math.h, stdlib.hand never cmath, cstdlibetc. I don't understand the reason the latter even exist, could someone please enlighten me?

在 C++ 程序中包含前者而不是后者的考虑因素是什么?我总是包括math.hstdlib.h从不cmathcstdlib等等。我不明白后者甚至存在的原因,有人可以启发我吗?

回答by Joseph Mansfield

Prefer to include the <c ...>headers. They are C++ standard library headers. The <... .h>headers are headers defined by the C standard library:

最好包含<c ...>标题。它们是 C++ 标准库头文件。<... .h>头文件是由 C 标准库定义的头文件:

The C++ standard library also makes available the facilities of the C standard library, suitably adjusted to ensure static type safety.

C++ 标准库还提供了 C 标准库的功能,经过适当调整以确保静态类型安全。

The C++ headers, for the most part, have content identical to the corresponding C library headers except that the names are all defined in the stdnamespace.

C++ 头文件的大部分内容与相应的 C 库头文件相同,只是名称都在std命名空间中定义。

Except as noted in Clauses 18 through 30 and Annex D, the contents of each header cnameshall be the same as that of the corresponding header name.h, as specified in the C standard library (1.2) or the C Unicode TR, as appropriate, as if by inclusion. In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace std. It is unspecified whether these names are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations

除了第 18 条到第 30 条和附件 D 中的说明外,每个头文件的内容cnamename.h与 C 标准库 (1.2) 或 C Unicode TR 中规定的相应头文件的内容相同,就像通过包容。但是,在 C++ 标准库中,声明(在 C 中定义为宏的名称除外)在命名空间 std 的命名空间范围 (3.3.6) 内。未指定这些名称是否首先在全局命名空间范围内声明,然后通过显式 using 声明注入命名空间 std