在C ++中,枚举的大小是否必须等于其基础类型的大小? [英] In C++, does the size of an enumeration have to be equal to the size of its underlying type?

查看:70
本文介绍了在C ++中,枚举的大小是否必须等于其基础类型的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常假设枚举的大小与其基础类型的大小相同。

I often assume that size of an enumeration is the same as the size of its underlying type. But is it mandated by the standard?

标准(C ++ 14,n4296)说每个枚举都有一个基础类型(7.2 / 5)。该标准还规定,对象用字节序列表示,并且对象的大小与其表示形式有关:

The standard (C++14, n4296) says that every enumeration has an underlying type (7.2/5). The standard also says that objects are represented as sequences of bytes, and that the size of an object is related to its representation:


3.9 / 4类型T的对象的对象表示形式是T类型的对象所占用的N
个无符号char对象的序列,其中N等于
sizeof(T)。

3.9/4 The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N equals sizeof(T).

5.3.3 / 1 sizeof运算符产生对象
中其操作数表示形式的字节数。

5.3.3/1 The sizeof operator yields the number of bytes in the object representation of its operand.

但是,我无法找到枚举的基础类型和对象表示形式之间的任何关系。有没有?如果不是,那么我会认为枚举的sizeof不必一定是其基础类型的sizeof。

However, I was not able to find any relation between an enum's underlying type and the object representation. Is there any? If not, than I would argue that sizeof of an enumeration does not have to be sizeof of its underlying type.

所以我的问题是:


  1. 枚举的基础类型与其对象表示形式之间是否存在任何关系?

  1. Is there any relation between an enum's underlying type and its object representation?

对于任何枚举E,标准是否真的要求 sizeof(std :: underlying_type_t< E>)== sizeof(E)

Does the standard really require that sizeof(std::underlying_type_t<E>) == sizeof(E) for any enumeration E?


推荐答案

来自: c ++枚举的基本类型是什么?,较旧的C ++标准在7.2 / 5中指出:

Taken from: What is the underlying type of a c++ enum?, An older C++ standard stated at 7.2/5:


枚举的基础类型是整数类型,可以使
代表枚举中定义的所有枚举器值。由
实现定义,该整数类型用作枚举的基础
类型,除非该基础类型不得比int大
,除非枚举器的值不适合在
int或unsigned int中。如果枚举数列表为空,则基础
类型就像枚举有一个值为0的单个枚举数。
sizeof()的值应用于枚举类型,即$的对象b $ b枚举类型或枚举数是将
应用于基础类型的sizeof()的值。

The underlying type of an enumeration is an integral type that can represent all the enumerator values defined in the enumeration. It is implementation-defined which integral type is used as the underlying type for an enumeration except that the underlying type shall not be larger than int unless the value of an enu- merator cannot fit in an int or unsigned int. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0. The value of sizeof() applied to an enu- meration type, an object of enumeration type, or an enumerator, is the value of sizeof() applied to the underlying type.

draft n4606 起,我能找到的最接近值是7.2 / 7 + 8,其中指出:

From draft n4606 the closest I could find is 7.2/7 + 8 which states:


7)对于基础类型不固定的枚举,基础
类型是整数类型,可以表示该枚举中定义的所有枚举值
。如果没有整数类型不能代表所有
枚举器值,则该枚举格式不正确。由
实现定义,该整数类型用作基础
类型,除非该基础类型不得大于int
,除非枚举器的值不能适合int或unsigned
int。如果枚举器列表为空,则基础类型就像
枚举具有一个值为0的单个枚举器。

7) For an enumeration whose underlying type is not fixed, the underlying type is an integral type that can represent all the enumerator values defined in the enumeration. If no integral type can represent all the enumerator values, the enumeration is ill-formed. It is implementation-defined which integral type is used as the underlying type except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0.

8)对于其基础的枚举类型是固定的,枚举的值是基础类型的
值。否则,对于
emin是最小的枚举数且emax是最大的枚举,该枚举的
的值是bmin到bmax范围内的值,定义为
如下:令K对于2的补码表示为1,对于
的补码或符号幅度表示为0。 bmax是大于或等于max(| emin |-K,| emax |)的
最小值,而
等于2M-1,其中M是一个非负整数。如果
emin为非负值,则bmin为零,否则为−(bmax + K)。如果bmin为零,则最小
最小位域的大小足以容纳
枚举类型的所有值,则大小为max(M,1),否则为M + 1。
可以定义一个枚举,该枚举的值不由其任何枚举​​器的
定义。如果枚举器列表为空,则枚举的
值就好像枚举中有一个枚举器,其枚举
的值为0

8) For an enumeration whose underlying type is fixed, the values of the enumeration are the values of the underlying type. Otherwise, for an enumeration where emin is the smallest enumerator and emax is the largest, the values of the enumeration are the values in the range bmin to bmax, defined as follows: Let K be 1 for a two’s complement representation and 0 for a ones’ complement or sign-magnitude representation. bmax is the smallest value greater than or equal to max(|emin| − K, |emax|) and equal to 2M − 1, where M is a non-negative integer. bmin is zero if emin is non-negative and −(bmax + K) otherwise. The size of the smallest bit-field large enough to hold all the values of the enumeration type is max(M, 1) if bmin is zero and M + 1 otherwise. It is possible to define an enumeration that has values not defined by any of its enumerators. If the enumerator-list is empty, the values of the enumeration are as if the enumeration had a single enumerator with value 0

一方面,它似乎足够接近,另一方面,对 sizeof()运算符的特定需求已被删除。
不过,我仍然可以肯定地说两个问题的答案都是肯定的。

On the one hand it seems close enough, on the other hand, the specific demand for the sizeof() operator was removed. Still I think it is safe enough to state that answer for both the questions is yes.

这篇关于在C ++中,枚举的大小是否必须等于其基础类型的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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