'operator|' 返回的类型并不总是与您传递给它的类型匹配
The type returned by 'operator|' does not always match the type you passed it
一些代码调用了模板函数,但它没有调用我期望的专业化。原来罪魁祸首是这样的:我对一些 uint16_t 常量进行或运算并将它们作为参数传递,如下所示:foo(uint16_bar | uint16_baz);
。我的期望是它会调用 foo<uint16_t>
,但它没有。我可以很容易地通过演员表来回避这个问题,但我真的很想知道这可能是什么原因。特别是因为并非您传递的每个整数类型都会 return 类型 int.
案例一:
uint16_t a = 2;
uint16_t b = 4;
auto x = (a|b); // x is of type 'int': Surprize!
案例二:
uint a = 2;
uint b = 4;
auto x = (a|b); // x is of type unsigned_int: What I would expect
为什么情况 1 中的 (a|b) 不是 return 类型 uint16_t 的值?
通常的算术转换应用于按位运算符 | 的操作数。这意味着等级小于 int
类型等级的整数对象将转换为 int
或 unsigned int
,因为整数提升是通常算术转换的一部分..
来自 C++ 14 标准(5.13 位包含或运算符)
1 The usual arithmetic conversions are performed; the result is the
bitwise inclusive OR function of its operands. The operator applies
only to integral or unscoped enumeration operands.
和(4.5 积分促销)
1 A prvalue of an integer type other than bool, char16_t, char32_t, or
wchar_t whose integer conversion rank (4.13) is less than the rank of
int can be converted to a prvalue of type int if int can represent all
the values of the source type; otherwise, the source prvalue can be
converted to a prvalue of type unsigned int
内置运算符的一个怪癖是它们将对小参数执行整数提升:
cppreference on implicit conversions:
prvalues of small integral types (such as char
) may be converted to prvalues of larger integral types (such as int
). In particular, arithmetic operators do not accept types smaller than int
as arguments, and integral promotions are automatically applied after lvalue-to-rvalue conversion, if applicable. This conversion always preserves the value.
在您的代码中,按位或运算符在应用运算符之前将您的 uint16_t
提升为 int
。
一些代码调用了模板函数,但它没有调用我期望的专业化。原来罪魁祸首是这样的:我对一些 uint16_t 常量进行或运算并将它们作为参数传递,如下所示:foo(uint16_bar | uint16_baz);
。我的期望是它会调用 foo<uint16_t>
,但它没有。我可以很容易地通过演员表来回避这个问题,但我真的很想知道这可能是什么原因。特别是因为并非您传递的每个整数类型都会 return 类型 int.
案例一:
uint16_t a = 2;
uint16_t b = 4;
auto x = (a|b); // x is of type 'int': Surprize!
案例二:
uint a = 2;
uint b = 4;
auto x = (a|b); // x is of type unsigned_int: What I would expect
为什么情况 1 中的 (a|b) 不是 return 类型 uint16_t 的值?
通常的算术转换应用于按位运算符 | 的操作数。这意味着等级小于 int
类型等级的整数对象将转换为 int
或 unsigned int
,因为整数提升是通常算术转换的一部分..
来自 C++ 14 标准(5.13 位包含或运算符)
1 The usual arithmetic conversions are performed; the result is the bitwise inclusive OR function of its operands. The operator applies only to integral or unscoped enumeration operands.
和(4.5 积分促销)
1 A prvalue of an integer type other than bool, char16_t, char32_t, or wchar_t whose integer conversion rank (4.13) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int
内置运算符的一个怪癖是它们将对小参数执行整数提升:
cppreference on implicit conversions:
prvalues of small integral types (such as
char
) may be converted to prvalues of larger integral types (such asint
). In particular, arithmetic operators do not accept types smaller thanint
as arguments, and integral promotions are automatically applied after lvalue-to-rvalue conversion, if applicable. This conversion always preserves the value.
在您的代码中,按位或运算符在应用运算符之前将您的 uint16_t
提升为 int
。