考虑以下代码:
// E1 << E2 with E1 bool, and E2 an integer
true << static_cast<char>(1);
true << static_cast<short int>(1);
true << static_cast<int>(1);
true << static_cast<unsigned int>(1);
true << static_cast<long long int>(1);
true << static_cast<long long unsigned int>(1);在执行此操作时,是将E1提升为与E2相同的类型,还是首先将其提升为int,然后提升为std::common_type<E1, E2>::type
换言之,是:
true << static_cast<char>(9);定义的行为还是未定义的行为?
发布于 2015-09-26 19:01:08
来自expr.shift
操作数应为整数或未限定范围的枚举类型,并进行积分提升。结果的类型是提升的左操作数的类型。
来自conv.prom
bool类型的prvalue可以转换为int类型的prvalue,false变为0,true变为1。
在bool上的整体推广生产int。因此,true << (any integral type)的结果类型是int。积分提升后的true << static_cast<char>(9)与1 << 9相同,定义为512.
https://stackoverflow.com/questions/32800866
复制相似问题