gpt4 book ai didi

c++ - boost 线程代码 ~uintmax_t(0)

转载 作者:行者123 更新时间:2023-11-30 03:03:49 25 4
gpt4 key购买 nike

bool is_sentinel() const
{
return milliseconds==~uintmax_t(0);
}

我在thread_data.hpp中找到了这行代码,我想知道为什么是~uintmax_t(0)而不是-1

编辑:

如果原因是为了避免编译器警告,为什么不使用:

std::numeric_limits(decltype(milliseconds)>::max()

?

最佳答案

首先使用 uintmax_t 的一个原因是我们不知道最大的类型是什么。是 unsigned long 还是 unsigned long long

我的猜测是,使用 ~uintmax_t(0) 产生一个大的无符号值只会在最多的编译器上产生最少数量的警告。

编译器通常会在您混合使用有符号和无符号值时发出警告,或者在无符号值上使用减号 (-1ull) 令人惊讶地 (?) 会得到无符号结果。

关于c++ - boost 线程代码 ~uintmax_t(0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9158601/

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