gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:47:58 26 4
gpt4 key购买 nike

此代码在 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 编译器下有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18574312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com