C++ Best Practices – Handling Unsigned Ints

cprogramming practices

I use unsigned ints everywhere, and I'm not sure if I should. This can be from database primary key id columns to counters, etc. If a number should never be negative, then I will always used an unsigned int.

However I notice from other's code that no one else seems to do this. Is there something crucial that I'm overlooking?

Edit: Since this question I've also noticed that in C, returning negative values for errors is commonplace rather than throwing exceptions as in C++.

Best Answer

Is there something crucial that I'm overlooking?

When calculations involve both signed and unsigned types as well as different sizes, the rules for type promotion can be complex and lead to unexpected behaviour.

I believe this is the main reason why Java omitted unsigned int types.

Related Topic