cstdio stdio.h命名空间

dan*_*nny 36 c++ header

我从c ++参考中看到这一行cstdio:

库中的每个元素都在std namespace. 但我尝试了代码:

std::printf("hello world"); 

printf("hello world");
Run Code Online (Sandbox Code Playgroud)

是否真的C++标头将名称放在std和全局名称空间中

Alo*_*ave 47

包括cstdio在名称std空间中导入符号名称,可能还包括在Global名称空间中.
包括stdio.h在Global名称空间中导入符号名称,也可能std名称空间中导入.

这同样适用于所有c风格的标题.


参考:
C++ 11标准

附件D(规范性)兼容性特征[depr]指出:

D.6 C标准库头

1为了与C标准库和C Unicode TR兼容,C++标准库提供了25个C头,如表151所示.

其中包括:

<assert.h> <float.h> <math.h> <stddef.h> <tgmath.h> <complex.h> <inttypes.h> <setjmp.h> <stdio.h> <time.h> <ctype.h> <iso646.h> <signal.h> <stdint.h> <uchar.h> <errno.h> <limits.h> <stdarg.h> <stdlib.h> <wchar.h> <fenv.h> <locale.h> <stdbool.h> <string.h> <wctype.h>

进一步,

2 每个C标头(每个标头都有一个表单名称)的name.h行为就好像每个名称放置在标准库名称空间中的名称cname header都放在全局名称空间范围内.未指定是在名称空间std的名称空间作用域(3.3.6)中首先声明或定义这些名称,然后通过显式使用声明(7.3.3)将这些名称注入到全局名称空间作用域中.

3 [示例:标头<cstdlib>肯定在命名空间std中提供其声明和定义.它还可以在全局命名空间中提供这些名称.标题<stdlib.h>肯定在全局命名空间中提供相同的声明和定义,就像在C标准中一样.它还可以在命名空间std中提供这些名称. - 末端的例子]


del*_*ver 9

根据libstdc ++ docs:

该标准规定,如果一个包含C风格的标题(<math.h>在这种情况下),符号将在全局命名空间中可用,并且可能在命名空间std ::(但这不再是一个公司要求).另一方面,包括C++样式的header(<cmath>)保证可以在namespace std中找到实体,也可以 在全局命名空间中找到实体.

http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt01ch03s02.html

  • @KrishnaOza 不正确。请参阅[此处](/sf/ask/5012311811/)。 (2认同)