在VC10编译器下是std :: common_type错误吗?

时间:2013-09-02 13:20:16

标签: c++

此代码在VC11下为两个检查打印出“unsigned short”,但第一个检查在VC10下打印“int”。我一直希望std::common_type<T,T>::typeT。这是一个错误还是允许的行为?也许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;
}

1 个答案:

答案 0 :(得分:3)

是的,它是错误的。 std :: common_type是VC10中的一种黑客,它使用类型提升,使sizeof小于sizeof(int)的任何内容都提升为int

std::_Ipromo<unsigned short>::_Type _PromoTy0;

_PromoTy0 is an int.
相关问题