C ++标准库函数在std ::命名空间中的C ++版本? [英] Are C++ versions of C standard library functions in the std:: namespace?
问题描述
C ++ Primer(第5版)在 91
页上说明了包含 C ++
版本 C 标准库头而不是
.h
版本:这种方式包含的名称最终在 std ::
命名空间,并且不会污染全局命名空间。
"C++ Primer" (5th edition) states on page 91
the advantage of including the C++
version of a C
standard library header instead of the .h
version: this way the included names end up in the std::
namespace and do not pollute the global namespace.
我试过包括 cstdio
,并惊讶地发现我可以使用 printf()
,而不指定 std ::
。有趣的是,只包括 iostream
或只有 string
也足以访问全局 printf ()
。我缺少一些东西?
I tried including cstdio
and was surprised to observe that I can use printf()
without specifying std::
. Interestingly, including only iostream
or only string
is also sufficient to get access to a global printf()
. Am I missing something?
我正在使用 g ++ 4.8.2
与 -Wextra -Werror -std = c ++ 11
。
推荐答案
未指定是否< cfoo>
也将名称放入全局命名空间。所有C库名称都保留在C ++的全局命名空间中。
It is unspecified whether <cfoo>
also puts the names into the global namespace. All C library names are reserved in the global namespace in C++.
[extern.names] / 3:
[extern.names]/3:
使用外部链接声明的标准C库中的每个名称都保留给实现,用作 externC
链接的名称,在命名空间std和全局命名空间中。
Each name from the Standard C library declared with external linkage is reserved to the implementation for use as a name with
extern "C"
linkage, both in namespace std and in the global namespace.
要重述:
- 要在
std
中使用名称,必须包含正确的标题。 - 您不必在上述上下文中重新定义来自C标准库的任何名称。
- 没有说明C标准库中的任何特定名称(除非您明确包含已弃用的标头
< foo.h>
)。
- in order to use a name in
std
, you must include the correct header. - You must not redefine any names from the C standard library in the above contexts.
- Nothing says whether any particular names from the C standard library are declared (unless you explicitly include the deprecated header
<foo.h>
).
这篇关于C ++标准库函数在std ::命名空间中的C ++版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!