如 Why is nullptr_t not a keyword 中所述,最好避免引入新的关键字,因为它们会破坏向后兼容性。
为什么 char16_t
和 char32_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 asuint_least16_t
. Likewise, definechar32_t
to be a distinct new type, that has the same size and representation asuint_least32_t
.[N1040 defined
char16_t
andchar32_t
as typedefs touint_least16_t
anduint_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
, andwchar_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/