Why the sizeof(bool) is not defined to be one, by the Standard itself?

前端 未结 4 600
长情又很酷
长情又很酷 2020-11-30 10:52

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

4条回答
  •  庸人自扰
    2020-11-30 11:38

    The other likely size for it is that of int, being the "efficient" integer type for the platform.

    On architectures where it makes any difference whether the implementation chooses 1 or sizeof(int) there could be a trade-off between size (but if you're happy to waste 7 bits per bool, why shouldn't you be happy to waste 31? Use bitfields when size matters) vs. performance (but when is storing and loading bool values going to be a genuine performance issue? Use int explicitly when speed matters). So implementation flexibility wins - if for some reason 1 would be atrocious in terms of performance or code size, it can avoid it.

提交回复
热议问题