使用 #include<filename> 的区别和#include<filename.h>在 C++ 中
使用 #include
What is the difference between using #include<filename> and #include<filename.h
> in C++? Which of the two is used and why is it is used?
推荐答案
C++ only include-files not found in the C standard never used filename.h
.自从第一个 C++ 标准问世(1998 年)以来,他们就使用 filename
作为自己的标头.
C++ only include-files not found in the C standard never used filename.h
. Since the very first C++ Standard came out (1998) they have used filename
for their own headers.
由 C 标准继承的文件变为 cfilename
而不是 filename.h
.像 filename.h
这样继承使用的 C 文件已被弃用,但仍是 C++ 标准的一部分.
Files inherited by the C Standard became cfilename
instead of filename.h
. The C files inherited used like filename.h
are deprecated, but still part of the C++ standard.
不同之处在于,在 C++ 中未定义为宏的名称位于命名空间 std::
中的 cfilename
中,而名称位于 filename.h
在全局命名空间范围内.所以你会在 stddef.h 中找到 ::size_t
,在 cstddef 中找到 std::size_t
.两者都是标准 C++,但不推荐使用 ::size_t(参见 C++ 标准的附录 D).
The difference is that names not defined as macros in C are found within namespace std::
in cfilename
in C++, while names in filename.h
are within the global namespace scope. So you will find ::size_t
in stddef.h, and std::size_t
in cstddef. Both are Standard C++, but use of ::size_t is deprecated (See Annex D of the C++ Standard).
这就是区别.
- 与 C 编译器的兼容性
- 与非常古老的 C++ 编译器的兼容性
- 名称在命名空间
std::
中.不再有名称冲突. - 新的 C++ 功能(例如浮点、长整数的重载数学函数)
- C 兼容性标头 (
filename.h
) 将来可能会消失.
- Names are within namespace
std::
. No name-clashes anymore. - New C++ features (e.g. overloaded math functions for float, long)
- C Compatibility Headers (
filename.h
) could disappear in future.
相关文章