Sign of C++ Enum Type Incorrect After Converting to Integral Type

心已入冬 提交于 2019-12-10 17:43:23

问题


My understanding is that C++ enumerations are converted to integral types according to Integral Promotion. And during Integral Promotion, we should try converting a value to int first and if the value cannot be represented by an int, unsigned int should be used:

C++03 conv.prom:

2) .... An rvalue of an enumeration type (7.2) can be converted to an rvalue of the first of the following types that can represent all the values of the enumeration (i.e. the values in the range bmin to bmax as described in 7.2: int, unsigned int, long, or unsigned long.

But my testing in VC++2010 shows contradicting result:

enum {A1=60, A2=61,A3=63,A4=64,A5=66,A6=0xffffFFF1};
const bool b1 = A6 < A1;
//result b1=true;

A6 is 0xffffFFF1 which should be an unsigned value (decimal 4294967281). And, since this value cannot be represented by an int, it should be converted to unsigned int. But clearly, when used in the comparison, A6 was converted to -15. Why is it so?


回答1:


It is a bug of MS VC++. I described it here

Though the description is written in Russian you can translate it into English using for example google service translate.

Also there is a defect relative to enumerations in the C++ Standard itself. I also described it here

And one more reference relative to a discussion of enumerations (at this time in English) can be accessed here



来源:https://stackoverflow.com/questions/24802322/sign-of-c-enum-type-incorrect-after-converting-to-integral-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!