简体   繁体   English

在C ++标准中,它表示sizeof(wchar_t)<= sizeof(long)和sizeof(bool)<= sizeof(long)?

[英]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) Stroustrup先生在他的新书(TCPL第4版)的第149页上写了以下内容

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) . 我可以对sizeof(bool) <= sizeof(long)说同样的话。

Edit : In 3.9.1p5 you'll find: 编辑 :在3.9.1p5你会发现:

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. 类型wchar_t应具有与其他整数类型之一相同的大小,符号和对齐要求(3.11),称为其基础类型。

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 . 对于sizeof(wchar_t)>N && sizeof(long)<=N的任何值,很难想象sizeof(wchar_t)>N && sizeof(long)<=N实现,更不用说任何人都会考虑使用wchar_t That said: 那说:

In fact, I think it's wrong. 事实上,我认为这是错误的。 4.5 "Integral promotions" p2 explicitly allows for the possibility: 4.5“整体促销”p2明确允许:

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. 2.将char16_t,char32_t或wchar_t(3.9.1)类型的prvalue转换为以下第一种类型的prvalue,它们可以表示其基础类型的所有值:int,unsigned int,long int,unsigned long int,long long int或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. 如果该列表中的所有类型都不能表示其基础类型的所有值,则可以将char16_t,char32_t或wchar_t类型的prvalue转换为其基础类型的prvalue。

and 5.3.3 "sizeof" p1 explicitly leaves the actual sizes implementation-defined: 5.3.3“sizeof”p1明确地保留了实现定义的实际大小:

1. [...] The result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. 1. [...]应用于任何其他基本类型(3.9.1)的sizeof的结果是实现定义的。 [ *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. [ *注意:特别是sizeof(bool),sizeof(char16_t),sizeof(char32_t)和sizeof(wchar_t)是实现定义的。* 76 - 结束注释] [注意:有关byte和3.9的定义,请参阅1.7用于对象表示的定义。 — 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. 虽然5.3.3足以让相对大小值得怀疑,但我认为wchar_t在任何标准类型中无法代表的规范可能性使其无可置疑。

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

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