在比较UINT16_T和UNSIGNED INT时,为什么存在签名问题?

发布于 2025-01-27 06:59:22 字数 756 浏览 2 评论 0原文

我有这样的代码:

#include <iostream>
using std::cout;
using std::endl;

int main() {
    uint16_t a = 0;
    uint16_t b = 0;

    if ( a - b < 3u )
    {
        cout << "cacahuète" << endl;
    }
    return 0;
}

当我使用-wall对G ++进行编译时,我会得到:

temp.cpp: In function ‘int main()’:
temp.cpp:9:13: warning: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Wsign-compare]
    9 |  if ( a - b < 3u )
      |       ~~~~~~^~~~

如果我编写,则不会显示该警告,如果(a -b&lt; static_cast&static_cast&lt; uint16_t&gt ;(3U))而不是。

  • 那是怎么回事? INT来自哪里?
  • 这实际上会导致不正确的行为吗?
  • 是否有较少的详细方法来使它保持沉默? (还是写uint16_t字面的详细方法?)

I have a code like this :

#include <iostream>
using std::cout;
using std::endl;

int main() {
    uint16_t a = 0;
    uint16_t b = 0;

    if ( a - b < 3u )
    {
        cout << "cacahuète" << endl;
    }
    return 0;
}

When I compile it using g++ with -Wall, I get :

temp.cpp: In function ‘int main()’:
temp.cpp:9:13: warning: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Wsign-compare]
    9 |  if ( a - b < 3u )
      |       ~~~~~~^~~~

That warning doesn't show up if I write if ( a - b < static_cast<uint16_t>(3u) ) instead.

  • So what's going on, here ? Where does the int come from?
  • Can this actually result in an incorrect behavior?
  • Is there a less verbose way to silence it? (or a less verbose way to write a uint16_t literal?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见终念 2025-02-03 06:59:22

那是怎么回事? int来自哪里?

整数促销正在这里进行。在std :: uint16_t的系统上,它小于int,将其升级为int(大多数二进制操作)时。

a -b中,两个操作数均升级为int,结果是int。您将此签名的整数与3U进行比较,该整数是insted int。迹象有所不同,因为编译器警告您。

如果我编写,则不会显示警告,如果(a -b&lt; static_cast&lt; uint16_t&gt;(3U))而不是。

在这里,右手操作数也被提升为int。比较的两面都签署了,因此没有警告。

这实际上会导致不正确的行为吗?

如果(a -b&lt; static_cast&lt; uint16_t&gt;(3U))的行为确实不同于a -b&lt; static_cast&lt; uint16_t&gt;(3U)。如果一个是正确的,那么大概另一个是不正确的。

是否有较少的详细方法使它保持沉默? (还是写uint16_t文字的详细方法?)

正确的解决方案取决于您想要正确的行为。


ps您忘了包含定义uint16_t的标题。

So what's going on, here ? Where does the int come from?

Integer promotion is going on here. On systems where std::uint16_t is smaller than int, it will be promoted to int when used as an operand (of most binary operations).

In a - b both operands are promoted to int and the result is int also. You compare this signed integer to 3u which is unsigned int. The signs differ, as the compiler warns you.

That warning doesn't show up if I write if ( a - b < static_cast<uint16_t>(3u) ) instead.

Here, the right hand operand is also promoted to int. Both sides of comparison are signed so there is no warning.

Can this actually result in an incorrect behavior?

if ( a - b < static_cast<uint16_t>(3u) ) does have different behaviour than a - b < static_cast<uint16_t>(3u). If one is correct, then presumably the other is incorrect.

Is there a less verbose way to silence it? (or a less verbose way to write a uint16_t literal?)

The correct solution depends on what behaviour you want to be correct.


P.S. You forgot to include the header that defines uint16_t.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文