从 int 到 uint8_t 的隐藏缩小转换

Hidden narrowing conversion from int to uint8_t

考虑以下代码:

#include <cstdint>

class A
{
public:

    explicit A(uint8_t p_a){ m_a = p_a; };
    uint8_t get_a() const {return m_a;}

private:

    uint8_t m_a;
};

int main()
{
    A a {0x21U};
    A aa{0x55U};

    uint8_t mask{a.get_a() | aa.get_a()};

    return 0;
}

当我尝试编译此 (gcc 5.4.0) 时,出现以下错误:

main.cpp: In function ‘int main()’:
main.cpp:20:28: warning: narrowing conversion of ‘(int)(a.A::get_a() | aa.A::get_a())’ from ‘int’ to ‘uint8_t {aka unsigned char}’ inside { } [-Wnarrowing]
     uint8_t mask{a.get_a() | aa.get_a()};

我真的不明白为什么会缩小。 int 类型从未在我的代码中的任何地方使用过,所有内容都是根据 unsigned char 编写的。即使我明确转换为 unsigned char 我也会收到错误消息:

uint8_t mask{static_cast<uint8_t>(a.get_a()) | static_cast<uint8_t>(aa.get_a())};

事实上,要解决这个问题,我需要删除{}-初始化。然后它起作用了:

uint8_t mask = a.get_a() | aa.get_a();

为什么这是必要的?

Integral promotion 案例:

prvalues of small integral types (such as char) may be converted to prvalues of larger integral types (such as int).

a.get_a() | aa.get_a() - 这是纯右值表达式

列表初始化更加严格,这就是您收到警告的原因。

uint8_t mask{a.get_a() | aa.get_a()};

大括号内的表达式

auto i = a.get_a() | aa.get_a(); // i is int

提升为 int,由于 int 不能完全适合 uint8_t,因此根据 this rule:

发出缩小警告

If the initializer clause is an expression, implicit conversions are allowed as per copy-initialization, except if they are narrowing (as in list-initialization) (since C++11).

你很接近这个:

uint8_t mask{static_cast<uint8_t>(a.get_a()) | static_cast<uint8_t>(aa.get_a())};

但是 a.get_a()aa.get_a() 已经是 uint8_t,所以转换什么都不做。

这是 | 操作:

  • 将两个操作数都转换为int(在你可以做任何事情之后)
  • 评估为 int

所以这是您现在需要随后转换的整个表达式:

uint8_t mask{static_cast<uint8_t>(a.get_a() | aa.get_a())};

您尝试删除 {} 初始化也是正确的,这可能也是我会做的。你在这里不需要它的严格性。

uint8_t mask = a.get_a() | aa.get_a();

这是清楚、简洁和正确的。

大多数二进制算术运算,包括此处出现的 | bitwise-or 都会强制提升它们的子表达式,也就是说它们至少会是 intunsigned int排名。

C++ 17 [expr] 第 11 段:

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

  • If either operand is of scoped enumeration type, ...

  • If either operand is of type long double, ...

  • Otherwise, if either operand is double, ...

  • Otherwise, if either operand is float, ...

  • Otherwise, the integral promotions shall be performed on both operands. Then the following rules shall be applied to the promoted operands: ...

这里的 integral promotions 是导致 get_a() 值从 uint8_t 变为 int 的原因。所以 | 表达式的结果也是一个 int,并且缩小它以初始化另一个 uint8_t 是 ill-formed.