c++ – Why CPU_SET takes integer instead of unsigned interger as it’s first argument?

You misunderstand the difference between unsigned int and int. That is understandable; even the authors of C++ had the same problem.

You think unsigned int means int restricted to non-negative values. What unsigned int is actually is an integer mod 2^k for some unspecified k (often 32). Note I used integer here and not int.

int meanwhile is an integer bounded by (inclusively) -2^(k-1) and -1+2^(k-1), with behavior once it leaves that area undefined.

For most purposes, int gives a better experience. You can do things like “a-b” more often and get a non-surprising answer for modest sized a and b.

When you do this with an unsigned int you get results like a-b > a when both a and b are positive, which is algebraically surprising.

To this end, the C++ committee considers most uses of unsigned in the C++ standard to have been an error. As a concrete example, it is generally held that std::vector<>.size() should have returned a signed integer. The benefit of an extra bit of potential size is far overweighed by the insane number of errors it being a modulo-2^32 value has caused.

From this perspective, CPU-id is (a) not a modulo-integer, and (b) unlikely to require more than 2^31 different values (or even 2^15, the smallest limit on int size allowed). So using int here is probably wise.

Read more here: Source link