c++ - std::common_type 在 VC10 编译器下有 bug 吗?

标签 c++

此代码在 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/

相关文章:

python - 如何从 cv::Mat(在 C++ 中)转换为 tf.placeholder(在 C++ Boost.Python 中)?

c++ - C++11 中的默认初始化?

c++ - 如何在不推送的情况下创建 vector<string> 或类似的?

c++ - 如何将 C API 封装到 RAII C++ 类中?

c++ - 如何将 std::enable_if 与自推断返回类型一起使用?

c++ - 为什么此代码在模板特化方面有错误

c++ - 检测应用程序 C++ 的双重执行

支持 UTF-8 的 C++ 解析库

c++ - boost 库中是否有直接生成 png 文件而不是点的函数?

c++ - 模板继承的 VC++ 错误