C++标准p692:
In C++,the type of an enumerator is its enumeration. In C,tye type of an enumerator is int.
enum e{A};
sizeof(A)==sizeof(int) //in C
sizeof(A)==sizeof(e) //In C++
/*and sizeof(int) is not nessary equal to sizeof(e) */
那么怎么计算enumerator的sizeof?
the c++ programming language 3rd edition 4.8
The range of an enumeration holds all the enumeration’s enumerator values rounded up to the nearest larger binary power minus 1. The range goes down to 0 if the smallest enumerator is nonnegative and to the nearest lesser negative binary power if the smallest enumerator is negative. This defines the smallest bitfield capable of holding the enumerator values. For example:
enum e1 { dark, light}; / / range 0:1
enum e2 { a = 3, b = 9}; / / range 0:15
enum e3 {min= 10, max = 1000000 }; / / range 1048576:1048575
The sizeof an enumeration is the sizeof some integral type that can hold its range and not larger than sizeof(int), unless an enumerator cannot be represented as an int or as an unsigned int. For example, sizeof(e1) could be 1 or maybe 4 but not 8 on a machine where sizeof(int)==4.
In C++,the type of an enumerator is its enumeration. In C,tye type of an enumerator is int.
enum e{A};
sizeof(A)==sizeof(int) //in C
sizeof(A)==sizeof(e) //In C++
/*and sizeof(int) is not nessary equal to sizeof(e) */
那么怎么计算enumerator的sizeof?
the c++ programming language 3rd edition 4.8
The range of an enumeration holds all the enumeration’s enumerator values rounded up to the nearest larger binary power minus 1. The range goes down to 0 if the smallest enumerator is nonnegative and to the nearest lesser negative binary power if the smallest enumerator is negative. This defines the smallest bitfield capable of holding the enumerator values. For example:
enum e1 { dark, light}; / / range 0:1
enum e2 { a = 3, b = 9}; / / range 0:15
enum e3 {min= 10, max = 1000000 }; / / range 1048576:1048575
The sizeof an enumeration is the sizeof some integral type that can hold its range and not larger than sizeof(int), unless an enumerator cannot be represented as an int or as an unsigned int. For example, sizeof(e1) could be 1 or maybe 4 but not 8 on a machine where sizeof(int)==4.