c++ - `nullptr_t` 应该是全局命名空间的一部分吗?

标签 c++ c++11 language-lawyer

我看到微软的 stddef.h 定义 nullptr_t 是这样的:

namespace std
{
    typedef decltype(__nullptr) nullptr_t;
}

using ::std::nullptr_t;

using declerationnullptr_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 corresponding cname 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/

相关文章:

c++ - 使字段函数通用

c++ - ISO C++标准草案

c++ - 为什么 decltype 不是隐含的?

c++ - 自定义文字适用于 long double 但不适用于 double,并且适用于按值传递但不按引用传递

c++ - 关于简历分解的困惑

c++ - 复制图像后在 imshow 中断言失败 (size.width>0 && size.height>0)

c++ - 一个用于插入类对象的某些特定值的衬里

c++ - FFMPEG RTSP 使用 libx264 流到 MPEG4/H264 文件

c - 如何用 extern 解决新声明的多个先前声明?

c++ - 为什么没有为 C++14 数字分隔符选择空格字符?