1
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 ?

EDIT:

if reason is to avoid compiler warnings, why don't use :

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

?

Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145
  • 1
    [Related, but not an answer](http://stackoverflow.com/questions/809227/is-it-safe-to-use-1-to-set-all-bits-to-true) – CB Bailey Feb 06 '12 at 10:23

3 Answers3

2

One reason for using uintmax_t in the first place is that we don't know what the largest type is. Is it unsigned long or 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.

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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
1

~uintmax_t(0) is the simplest way to produce an all-ones value of type uintmax_t that does not produce compiler warnings.

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
0

Since milliseconds is unsigned, comparing it to -1 wouldn't make any sense.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278