简体   繁体   English

提升线程代码〜uintmax_t(0)

[英]boost thread code ~uintmax_t(0)

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

I have found this line of code in thread_data.hpp, I am wondering why is it ~uintmax_t(0) instead of -1 ? 我在thread_data.hpp中找到了这行代码,我想知道为什么它是~uintmax_t(0)而不是-1

EDIT: 编辑:

if reason is to avoid compiler warnings, why don't use : 如果原因是为了避免编译器警告,为什么不使用:

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

?

One reason for using uintmax_t in the first place is that we don't know what the largest type is. 首先使用uintmax_t的一个原因是我们不知道最大的类型是什么。 Is it unsigned long or unsigned long long ? unsigned long还是unsigned long long吗?

My guess is that using ~uintmax_t(0) to produce a large unsigned value simply produces the least number of warnings on the largest number of compilers. 我的猜测是,使用~uintmax_t(0)产生较大的无符号值只会在数量最多的编译器上产生最少数量的警告。

It is common for compilers to warn if you mix signed and unsigned values, or that using a minus on an unsigned value ( -1ull ) surprisingly(?) gives an unsigned result. 如果您混合使用带符号和无符号值,或者在带符号的负值( -1ull )上使用减号会令人惊讶地出现(?),则通常会向编译器发出警告。

~uintmax_t(0)是产生不产生编译器警告的uintmax_t类型的全值的最简单方法。

由于milliseconds是无符号的,因此将其与-1比较没有任何意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM