c++ - 如果 nullptr_t 不是关键字,为什么是 char16_t 和 char32_t?

标签 c++ c++11

Why is nullptr_t not a keyword 中所述,最好避免引入新的关键字,因为它们会破坏向后兼容性。

为什么 char16_tchar32_t 关键字也可以这样定义?

namespace std {
    typedef decltype(u'q') char16_t;
    typedef decltype(U'q') char32_t;
}

最佳答案

The proposal itself解释原因:允许使用 uint_least16_t 的基础类型重载和 uint_least32_t .如果他们是 typedef这是不可能的。

Define char16_t to be a distinct new type, that has the same size and representation as uint_least16_t. Likewise, define char32_t to be a distinct new type, that has the same size and representation as uint_least32_t.

[N1040 defined char16_t and char32_t as typedefs to uint_least16_t and uint_least32_t, which make overloading on these characters impossible.]

至于为什么他们不在std命名空间,这是为了与原始 C 提案兼容。 C++ 禁止 C 定义出现在它自己的 <cuchar> 版本中。

[c.strings]/3

The headers shall not define the types char16_t, char32_t, and wchar_t (2.11).

然后类型需要是全局类型定义,它带有自己的一组问题,例如

typedef decltype(u'q') char16_t;

namespace foo {
  typedef int char16_t;
}

std::nullptr_t 的原因不是关键字可以在您链接的问题中找到

We do not expect to see much direct use of nullptr_t in real programs.

制作 nullptr_t真正的异常(exception)在这里。

关于c++ - 如果 nullptr_t 不是关键字,为什么是 char16_t 和 char32_t?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37496787/

相关文章:

c++ - 将 openCV .dll 文件添加到 Netbeans C++ Qt 应用程序

c++ - 如何在Clion中使用虚拟环境从C++调用Python?

c++ - 为什么Boost.Concept通过空指针调用析构函数?

c++ - 如何使用 C++11 标准库生成随机数

c++ - 更改另一个类的值

c++ - 重新抛出异常保留回溯

c++ - 具有特定参数列表的函数指针

C++ 排序 lambda 比较器 EXC_BAD_ACCESS

c++ - 使用 SWIG 处理 std::function

c++ - 使用 Qt 制作静态构建(独立应用程序)