Is there any c++ standard paragraph which says that using -1 for this is portable and correct way or the only way of doing this correctly is using predefined values?
I have had a conversation with my colleague, what is better: using -1 for a maximum unsigned integer number or using a value from limits.h or std::numeric_limits ?
I have told my colleague that using predefined maximum values from limits.h or std::numeric_limits is the portable and clean way of doing this, however, the colleague objected to -1 being as same portable as numeric limits, and more, it has one more advantage:
unsigned short i = -1; // unsigned short max
can easily be changed to any other type, like
unsigned long i = -1; // unsigned long max
when using the predefined value from the limits.h header file or std::numeric_limits also requires to rewrite it too along with the type to the left.
-andunsignedon the same line is guaranteed to raise a few eyebrows.auto.-1.-1might be technically correct (see Eric's answer), from a clean code standpoint it isn't. Figuring out whether-1is an error here took you a question, and Eric a looking-up in the standard.unsigned short i = USHRT_MAXwould require neither, and be more explicit about the statement's intended purpose.