cstdio stdio.h 命名空间 [英] cstdio stdio.h namespace

查看:29
本文介绍了cstdio stdio.h 命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I see this line from the c++ reference for cstdio:

库的每个元素都在 std 命名空间中定义.但我尝试了代码:

Every element of the library is defined within the std namespace. but I tried the code:

std::printf("hello world"); 

printf("hello world");

C++ 标头是否将名称同时放在 std 和全局命名空间中?

推荐答案

包含 cstdio 会在 std 命名空间和 可能 中导入符号名称全局命名空间.
包含 stdio.h 会在全局命名空间中导入符号名称,可能会在 std 命名空间中导入符号名称.

Including cstdio imports the symbol names in std namespace and possibly in Global namespace.
Including stdio.h imports the symbol names in Global namespace and possibly in std namespace.

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

The same applies for all c-styled headers.

参考:
C++11 标准

Reference:
C++11 standard

附录 D(规范性)兼容性特性 [depr] 规定:

D.6 C 标准库头文件

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

1 For compatibility with the C standard library and the C Unicode TR, the C++ standard library provides the 25 C headers, as shown in Table 151.

其中包括:

<代码><assert.h><float.h><数学.h><stddef.h><tgmath.h><代码><complex.h><inttypes.h><setjmp.h><stdio.h><time.h><代码><ctype.h><iso646.h><信号.h><stdint.h><uchar.h><代码><errno.h><limits.h><stdarg.h><stdlib.h><wchar.h><代码><fenv.h><locale.h><stdbool.h><字符串.h><wctype.h>

继续,

2 每个 C 头文件都有一个 name.h 形式的名称,其行为就像每个名称由相应的 cname 头文件放置在标准库命名空间中一样 放置在全局命名空间范围内. 未指定这些名称是否首先在命名空间 std 的命名空间范围(3.3.6)内声明或定义,然后通过显式注入到全局命名空间范围内使用声明 (7.3.3).

2 Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).

3 [ 示例:标题 确实在命名空间 std 中提供了它的声明和定义.它还可以在全局命名空间中提供这些名称.头文件 <stdlib.h> 确实在全局命名空间中提供了与 C 标准中相同的声明和定义.它还可以在命名空间 std 中提供这些名称.——结束示例]

3 [ Example: The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std. —end example ]

这篇关于cstdio stdio.h 命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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