c++ - <c____> 头文件中声明的 C 函数是否保证在全局命名空间和 std 中?

标签 c++ c namespaces

所以这是我一直想知道但一直不太确定的事情。所以这完全是出于好奇,而不是真正的问题。

据我了解,当您执行 #include <cstdlib> 之类的操作时一切(当然宏除外)都在 std:: 中声明命名空间。我见过的每个实现都是通过执行以下操作来实现的:

#include <stdlib.h>
namespace std {
    using ::abort;
    // etc....
}

这当然具有全局命名空间和 std 中的效果。 .这种行为是否得到保证?或者是否有可能实现可以将这些东西放在 std 中?但不在全局命名空间中?我能想到的唯一方法是让你的 libstdc++ 实现每个 c 函数本身,将它们放在 std 中。直接而不是只包含现有的 libc header (因为没有从命名空间中删除某些内容的机制)。这当然是付出了很多努力,却几乎没有任何好处。

我的问题的本质是,以下程序是否严格符合并保证工作?

#include <cstdio>
int main() {
    ::printf("hello world\n");
}

编辑:我找到的最接近的是这个(17.4.1.2p4):

Except as noted in clauses 18 through 27, the contents of each header cname shall be the same as that of the corresponding header name.h, as specified in ISO/IEC 9899:1990 Programming Languages C (Clause 7), or ISO/IEC:1990 Programming Languages—C AMENDMENT 1: C Integrity, (Clause 7), as appropriate, as if by inclusion. In the C + + Standard Library, however, the declarations and definitions (except for names which are defined as macros in C) are within namespace scope (3.3.5) of the namespace std.

说实话,我可以用任何一种方式解释。 “每个 header cname 的内容应与相应 header name.h 的内容相同,如 ISO/IEC 9899:1990 Programming Languages C 中所指定的那样”告诉我它们可能在全局命名空间中是必需的,但是“在但是,C++ 标准库的声明和定义(名称除外) 在 C 中定义为宏)在命名空间 std 的命名空间范围内(3.3.5)。”表示它们在 std 中(但没有指定它们所在的任何其他范围)。

最佳答案

下面是 MSVC 团队 (http://blogs.msdn.com/vcblog/archive/2008/08/28/the-mallocator.aspx#8904359) 的 Stephan T. Lavavej 对情况的一个很好的概要(与标准所说的相比有些相关性):

> also, <cstddef>, <cstdlib>, and std::size_t etc should be used!

I used to be very careful about that. C++98 had a splendid dream wherein <cfoo> would declare everything within namespace std, and <foo.h> would include <cfoo> and then drag everything into the global namespace with using-declarations. (This is D.5 [depr.c.headers].)

This was ignored by lots of implementers (some of which had very little control over the C Standard Library headers). So, C++0x has been changed to match reality. As of the N2723 Working Paper, http://open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2723.pdf , now <cfoo> is guaranteed to declare everything within namespace std, and may or may not declare things within the global namespace. <foo.h> is the opposite: it is guaranteed to declare everything within the global namespace, and may or may not declare things within namespace std.

In reality and in C++0x, including <cfoo> is no safeguard against everything getting declared in the global namespace anyways. That's why I'm ceasing to bother with <cfoo>.

This was Library Issue 456, http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#456 .

(C++0x still deprecates the headers from the C Standard Library, which is hilarious.)

我自己从不喜欢 <cfoo> header ,发现我一直使用 <foo.h> 。现在我觉得我可以不再为我在这方面缺乏 C++“纯度”而焦虑了。

关于c++ - <c____> 头文件中声明的 C 函数是否保证在全局命名空间和 std 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2587445/

相关文章:

c++ - Gtkmm:如何暂停应用程序的执行并等待用户输入?

ios - typedef 重新定义错误 Xcode 5、iOS7 和 64 位与 32 位

.net - .aspx 页面后面代码的自定义命名空间

c++ - 将文件读入 std::vector<std::byte>

c++ - 在堆或堆栈中创建构造函数有什么区别?

c++ - 通过 TCS 和 TES 传递数据

c - 在 C 中查找主机地址范围

C 单链表使用 gcc 但不使用 dcc 出现段错误

c# - 使用这种类型的外部库后出现错误 "Type does not exist in the namespace"

module - Go 和命名空间 : is it possible to achieve something similar to Python?