3

Does the C++ standard provide any guarantee on the ordering of the size in bytes of char, wchar_t, char16_t, char32_t? (any extract from the standard is welcome)

For example do I have the guarantee that:

sizeof(char) <= sizeof(wchar_t) <= sizeof(char16_t) <= sizeof(char32_t)
Vincent
  • 57,703
  • 61
  • 205
  • 388

3 Answers3

2

It's 1 == sizeof(char) <= sizeof(wchar_t) and 1 == sizeof(char) <= sizeof(char16_t) <= sizeof(char32_t).

5.3.3/1 Sizeof [expr.sizeof]

... sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1. ... [ Note: in particular, sizeof(bool), sizeof(char16_t), sizeof(char32_t), and sizeof(wchar_t) are implementation-defined.75 — end note ].

3.9.1/5 Fundamental types [basic.fundamental]

... Type wchar_t shall have the same size, signedness, and alignment requirements (3.11) as one of the other integral types, called its underlying type. 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 <cstdint>, called the underlying types.

Update: I haven't found it in the standard. cppreference says for uint_leastN_t:

smallest unsigned integer type with width of at least 8, 16, 32 and 64 bits respectively

Note that sizeof(char)==1 does not mean that a char has 8 bits. See also C++ FAQ. cppreference says about CHAR_BIT:

number of bits in byte

1.7/1 The C ++ memory model [intro.memory]

The fundamental storage unit in the C ++ memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set (2.3) ...

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Florian Kaufmann
  • 803
  • 6
  • 13
  • I am not sure about the < size because the standard does not specify how much bits there is in a byte. If a byte is 16 bits long, then sizeof(char) == sizeof(char16_t), at least I think. It needs confirmation however... – Vincent Dec 06 '15 at 21:23
  • Right. I updated my answer. I was however unable to find something which states that a C++ byte and a char have the same amount of bits. – Florian Kaufmann Dec 06 '15 at 21:55
0

The types char16_t and char32_t are defined to be the same size as uint_least16_t and uint_least32_t respectively.

No such constraints exist on wchar_t, except that it must be at least as large as char (which, of course, is true for all data types in C and C++). There exist actual implementations with 1-byte, 2-byte (MSVC++), and 4-byte (GCC) wide characters.

dan04
  • 87,747
  • 23
  • 163
  • 198
0

In 64-bit Qt Creator (using Clang), wchar_t is four (4) bytes.

Pierre
  • 4,114
  • 2
  • 34
  • 39