我看到微软的 stddef.h
定义 nullptr_t
是这样的:
namespace std
{
typedef decltype(__nullptr) nullptr_t;
}
using ::std::nullptr_t;
using
decleration 将 nullptr_t
注入(inject)全局命名空间。我在标准中找不到任何内容表明应该这样做。
我还看到在 GCC 中 nullptr_t
不在全局命名空间中。
这两种实现都可以被允许还是其中之一是错误?
糟糕的是,GCC 的行为与 CL 相同。
编辑: cstddef
也会发生同样的情况,下面的代码可以用 VC (online too) 编译。
#include <cstddef>
int main()
{
nullptr_t nil = nullptr;
}
最佳答案
它必须在全局命名空间中。
D.5 C standard library headers [depr.c.headers]
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 correspondingcname
header is placed within the global namespace scope. [...]
这里对于特定于 C++ 的名称也不异常(exception)。如果 C++ 将名称添加到 <cstddef>
那个 C 的 <stddef.h>
不提供,那么 C++ 的 <stddef.h>
还是需要提供。
关于c++ - `nullptr_t` 应该是全局命名空间的一部分吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33101264/