Mat*_*lia 31
所有标准C++头文件.h最终都不需要.我读到某个地方的概念是它们不需要是实际的文件,即使我从未看过实现以另一种方式 编辑它: 实际上编译器内在函数应该考虑包含的头但不实际包含它们作为文件; 看@Yttrill的评论.
对于这stdio.h件事,在C++应用程序中你不应该包括<stdio.h>,但你应该包括<cstdio>.一般来说,你不应该包含"普通"的C头,但是他们的C++化的对应物(最终没有得到它们).h有一个c前置并将它们中定义的所有符号放在std命名空间中.所以,<math.h>成为<cmath>,<stdlib.h>成为<cstdlib>,等等.
一般来说,你应该使用C++的C头版本来避免污染全局命名空间(假设你不是那些放在using namespace std;任何地方的那些人)并且有利于对标准C头的一些C++改进(例如添加重载到一些数学函数).
matteo@teoubuntu:/usr/include/c++/4.4$ ls
algorithm cstdarg functional sstream
array cstdatomic initializer_list stack
backward cstdbool iomanip stdatomic.h
bits cstddef ios stdexcept
bitset cstdint iosfwd streambuf
c++0x_warning.h cstdio iostream string
cassert cstdlib istream system_error
ccomplex cstring iterator tgmath.h
cctype ctgmath limits thread
cerrno ctime list tr1
cfenv cwchar locale tr1_impl
cfloat cwctype map tuple
chrono cxxabi-forced.h memory typeinfo
cinttypes cxxabi.h mutex type_traits
ciso646 debug new unordered_map
climits deque numeric unordered_set
clocale exception ostream utility
cmath exception_defines.h parallel valarray
complex exception_ptr.h queue vector
complex.h ext random x86_64-linux-gnu
condition_variable fenv.h ratio
csetjmp forward_list regex
csignal fstream set
Run Code Online (Sandbox Code Playgroud)
理论上 C++化的C头可能只是一个
namespace std
{
#include <original_C_header.h>
};
Run Code Online (Sandbox Code Playgroud)
但一般来说,处理特定于实现的问题(特别是关于宏)以及添加与C++相关的功能(例如,参见前面添加的重载示例)会更复杂<cmath>.
<c***>标题应该表现得好像它们<***.h>在namespace std指令中包含了标题,而是相反:
为了与标准C库兼容,C++标准库提供了18个C头[...]每个C头,其名称的格式为name.h,就好像每个名称都由相应的cname放置在标准库命名空间中header也放在name-space std的命名空间范围内,后跟一个显式的using-declaration(7.3.3)
请注意,此类标头被视为已弃用(§C.2.1),因此这是您不应使用它们的主要原因:
C.2.1对头文件的修改 为了与标准C库兼容,C++标准库提供了18个C头文件(D.5),但在C++中不推荐使用它们.