为什么的sizeof(布尔)没有定义为一体,通过标准本身? [英] Why the sizeof(bool) is not defined to be one, by the Standard itself?

查看:133
本文介绍了为什么的sizeof(布尔)没有定义为一体,通过标准本身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尺寸字符符号字符 unsigned char型被定义为1个字节,由C ++标准本身。我不知道为什么它没有定义的sizeof(布尔)也?

Size of char, signed char and unsigned char is defined to be 1 byte, by the C++ Standard itself. I'm wondering why it didn't define the sizeof(bool) also?

C ++标准03 5.3.3 $ / 1说:

C++03 Standard $5.3.3/1 says,

的sizeof(炭),sizeof的(有符号字符)和
  的sizeof(unsigned char型)为1;该
  应用于sizeof的结果的其他
  基本类型(3.9.1)是
  实现定义。 [注:
  特别的sizeof(布尔)和
  的sizeof(wchar_t的)是
  实现定义
0.69)

sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. [Note: in particular,sizeof(bool) and sizeof(wchar_t) are implementation-defined.69)

据我所知,sizeof(bool)不能少于一个字节。但没有任何理由,为什么是大于1字节要么?我不是说实现定义它是大于1,但该标准把它通过执行这可能是大于1,如果定义。

I understand the rationale that sizeof(bool) cannot be less than one byte. But is there any rationale why it should be greater than 1 byte either? I'm not saying that implementations define it to be greater than 1, but the Standard left it to be defined by implementation as if it may be greater than 1.

如果没有理由的sizeof(布尔)是大于1的话,我不明白为什么标准并没有把它定义为刚 1个字节,因为它定义的sizeof(char)的,它的所有变体。

If there is no reason sizeof(bool) to be greater than 1, then I don't understand why the Standard didn't define it as just 1 byte, as it has defined sizeof(char), and it's all variants.

推荐答案

很多平台不能有效地加载超过32位的较小的值。他们必须加载32位,并使用一个移位 - 和 - 掩码操作来提取8比特。你不会希望这个单布尔 S,但它的字符串确定。

Many platforms cannot effectively load values smaller than 32 bits. They have to load 32 bits, and use a shift-and-mask operation to extract 8 bits. You wouldn't want this for single bools, but it's OK for strings.

这篇关于为什么的sizeof(布尔)没有定义为一体,通过标准本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆