简体   繁体   中英

Where in the C++ Standard does it say that sizeof(wchar_t) <= sizeof(long) and sizeof(bool) <= sizeof(long)?

Mr. Stroustrup wrote the following on page 149 of his new book (TCPL 4th edition)

1 <= sizeof(wchar_t) <= sizeof(long)

I couldn't find anything in the Standard supporting the last inequality above. I could say the same for sizeof(bool) <= sizeof(long) .

Edit : In 3.9.1p5 you'll find:

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.

which gives support to the inequality

sizeof(wchar_t) <= sizeof(long long)

but not to

sizeof(wchar_t) <= sizeof(long)

But I couldn't find anything confirming

sizeof(bool) <= sizeof(long)

It's hard to imagine a sizeof(wchar_t)>N && sizeof(long)<=N implementation for any value of N, let alone one on which anyone would consider using wchar_t . That said:

In fact, I think it's wrong. 4.5 "Integral promotions" p2 explicitly allows for the possibility:

2. A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted to a prvalue of the first of the following types that can represent all the values of its underlying type: int, unsigned int, long int, unsigned long int, long long int, or unsigned long long int. If none of the types in that list can represent all the values of its underlying type , a prvalue of type char16_t, char32_t, or wchar_t can be converted to a prvalue of its underlying type.

and 5.3.3 "sizeof" p1 explicitly leaves the actual sizes implementation-defined:

1. [...] The result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. [ *Note: in particular, sizeof(bool), sizeof(char16_t), sizeof(char32_t), and sizeof(wchar_t) are implementation-defined.* 76 — end note ] [ Note: See 1.7 for the definition of byte and 3.9 for the definition of object representation. — end note ]

While 5.3.3 is enough to leave the relative sizes open to question, the normative possibility of wchar_t being unrepresentable in any standard type I think makes it unquestionable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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