此代码在 VC11 下为两个检查打印出“unsigned short”,但在 VC10 下第一个检查打印为“int”。我总是期待 std::common_type<T,T>::type
成为T
。这是错误还是允许的行为?也许 VC10 的实现和最终的 C++11 标准之间的行为发生了变化?
#include <iostream>
#include <typeinfo>
#include <type_traits>
int main(int argc, const char* argv[])
{
unsigned short a = 1;
unsigned short b = 2;
auto c = true ? a : b;
std::cout << typeid(std::common_type<unsigned short, unsigned short>::type).name() << std::endl; // VC10: int
std::cout << typeid(c).name() << std::endl; // VC10: unsigned short
return 0;
}
最佳答案
是的,它有问题。 std::common_type 是 VC10 中的一个 hack,它使用类型提升,这样任何 sizeof
小于 sizeof(int)
的东西都会提升为 int
:
std::_Ipromo<unsigned short>::_Type _PromoTy0;
_PromoTy0 is an int.
关于c++ - std::common_type 在 VC10 编译器下有 bug 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18574312/