首先,std :: common_type(当然boost :: type_traits :: common_type)使用三元运算符来检索类型结果。在这种情况下,相关的报价来自CppReference,6B)
E2和E3已经算术或枚举型:常见的算术转换应用,使他们的常见类型,该类型是结果。
有了这个信息,我们可以找到一个通常的算术转换规则在c++ standard,5P10,第88页。
- 否则,如果具有无符号整数类型操作数的排名更高大于或等于另一个操作数的类型的等级,则带符号整数类型的操作数应转换为无符号整数类型的操作数的类型。
所以基本上你的问题的答案是:......因为标准是这样说的。
但是,你不是唯一一个发现这种行为出乎意料的人。这里有一个快速运行的例子尝试:
#include <iostream>
#include <typeinfo>
#include <type_traits>
int main(int argc, const char* argv[])
{
std::cout << typeid(std::common_type<char, unsigned char>::type).name() << std::endl;
// I would expect "short", and the result is "int", ok so far.
std::cout << typeid(std::common_type<short, unsigned short>::type).name() << std::endl;
// I would expect "int", and the result is "int", yay.
std::cout << typeid(std::common_type<int, unsigned int>::type).name() << std::endl;
// I would expect "long", but the result is "unsigned int"
std::cout << typeid(std::common_type<long, unsigned long>::type).name() << std::endl;
// I would expect "long long", but the result is "unsigned long"
// So this usual arithmetic conversion can lead to unexpected behavior:
auto var_auto = true ? var_i : var_ui;
std::cout << typeid(var_auto).name() << std::endl; // unsigned int
std::cout << var_auto << std::endl; // 4294967173
return 0;
}
但是,目前的行为是一个问题是known和proposal的存在是为了去除一些惊喜。
-Hannes
我不相信有任何保证'long long'将包含'unsigned long'的整个范围。如果它与其他规格说明一样,唯一的要求是它至少与“long”一样多。无论平台如何,类型提升都应该是一致的,所以对重载解析有一定的可预测性。 – jpm 2013-03-04 21:36:20
'std :: common_type'匹配确定三元运算符返回类型的规则。从这个角度来看,三元运算符返回比两个分支都大的类型显然是错误的。 – 2013-03-04 21:37:53
@KevinBallard我其实并不觉得这显然是错的。当分支返回不同的签名时,返回比其中任何一个分支更大的类型似乎是唯一可以保证无缺陷的事情。 – David 2013-03-04 21:52:36