Is it really needed to specify 0 as an unsigned integer? I mean 0 is always 0 regardless it's signed or not, no? In the below example is the 0U really needed?
There is a significant difference between the treatment of signed types vs the treatment of unsigned types. It just so happens that the value we are making unsigned is 0. By default, integer literals are signed.
It's to remove ambiguity that might be caused, when is "0" a signed number or unsigned number since it can be either.
1 2 3 4 5
void pup(signed) { std::cout << "s"; }
void pup(unsigned) { std::cout << "u"; }
pup(0); // what do you except to happen here ? Should the compiler
// just know what type you are thinking of ?
I think in that particular expression, the signedness of 0 is irrelevant. If it used any right shifts, that'd be a different story. For example, (int32_t)0x80000000 >> 1 == (int32_t)0xC0000000, but (uint32_t)0x80000000 >> 1 == (uint32_t)0x40000000.