简体   繁体   English

C ++ char16_t的大小取决于什么?

[英]C++ What does the size of char16_t depend on?

This is also related to char32_t and any intXX_t . 这也与char32_t和任何intXX_t The specification points out that: 规范指出:

2.14.3.2 : 2.14.3.2

The value of a char16_t literal containing a single c-char is equal to its ISO 10646 code point value, provided that the code point is representable with a single 16-bit code unit. 包含单个c-char的char16_t文字的值等于其ISO 10646代码点值,前提是代码点可用单个16位代码单元表示。

5.3.3.1 : 5.3.3.1

[..] in particular [..] sizeof(char16_t), sizeof(char32_t), and sizeof(wchar_t) are implementation-defined [..]特别是[...] sizeof(char16_t),sizeof(char32_t)和sizeof(wchar_t)是实现定义的

I can not see anything about the intXX_t types, apart from the comment that they are "optional" ( 18.4.1 ). 除了评论它们是“可选的”( 18.4.1 )之外,我看不出有关intXX_t类型的任何内容。

If a char16_t isn`t guaranteed to be 2 bytes, is it guaranteed to be 16 bit (even on architectures where 1 byte != 8 bit)? 如果char16_t保证为2个字节,那么它是否保证为16位(即使在1个字节的架构上!= 8位)?

3.9.1 Fundamental types [basic.fundamental] 3.9.1基本类型[basic.fundamental]

Types char16_t and char32_t denote distinct types with the same size, signedness, and alignment as uint_least16_t and uint_least32_t, respectively, in , called the underlying types. 类型char16_t和char32_t分别表示与uint_least16_t和uint_least32_t相同的大小,符号和对齐的不同类型,称为基础类型。

This means char16_t is at least 16 bits (but may be larger) 这意味着char16_t至少为16位(但可能更大)

But I also believe: 但我也相信:

The value of a char16_t literal containing a single c-char is equal to its ISO 10646 code point value, provided that the code point is representable with a single 16-bit code unit. 包含单个c-char的char16_t文字的值等于其ISO 10646代码点值,前提是代码点可用单个16位代码单元表示。

provides the same guarantees (though less explicitly (as you have to know that ISO 10646 is UCS (Note UCS is compatible but not exactly the same as Unicode))). 提供相同的保证(虽然不太明确(因为您必须知道ISO 10646是UCS(注意UCS是兼容的,但与Unicode不完全相同)))。

The value of a char16_t literal containing a single c-char is equal to its ISO 10646 code point value, provided that the code point is representable with a single 16-bit code unit. 包含单个c-char的char16_t文字的值等于其ISO 10646代码点值,前提是代码点可用单个16位代码单元表示。

This is impossible to satisfy if char16_t isn't at least 16 bits wide, so by contradiction, it's guaranteed to be at least that wide. 如果char16_t不是至少16位宽,那么这是不可能满足的,所以通过矛盾,它保证至少是那么宽。

It can't be guaranteed to be exactly 16 bits, since there are platforms which don't support types that small (for example, DSPs often can't address anything smaller than their word size, which may be 24, 32 or 64 bits). 它不能保证恰好是16位,因为有些平台不支持那么小的类型(例如,DSP通常不能处理小于它们的字大小的任何东西,可能是24位,32位或64位)。 Your first quote guarantees that it will be at least 16 bits. 您的第一个引用保证它至少为16位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM