4 ms·
C/C++ coding standards I've seen always warned against using unsigned to mean "cannot be negative", because of precisely this kind of bug. The standards I'm re
by willvarfar 10y ago
C/C++ coding standards I've seen always warned against using unsigned to mean "cannot be negative", because of precisely this kind of bug.
The standards I'm recalling were in-house standards, but a quick Google shows that Google have published their standard and they warn against unsigned too: https://google.github.io/styleguide/cppguide.html#Integer_Types https://google.github.io/styleguide/cppguide.html#Integer_Ty...
Bottom line: avoid unsigned for numbers. Use unsigned only for bags of bits.
- esrauch 10y agoI think the situation is a lot more nuanced than that. The issue was that additition overflow is undefined behavior for both signed ints and pointers (but it is defined for unsigned ints). The fix commit changes it to use an unsigned integer type instead, so it seems like a concrete example for the exact opposite of your suggestion.