Given this code,
unsigned short us = 0; int i = 0; auto sum = us + i;
is the type sum defined by the implementation? My reading of the C ++ 11 standard says yes:
- 5.7 / 1 says that normal arithmetic conversions apply.
- 4.13 / 1 cartridges 2 and 3 say that the int rank is greater than the unsigned short rank.
- 5/9 bullet 5 subbullet 4 says that if int can represent all values ββin unsigned short, unsigned short is converted to int, and type
sum is int. - 5/9 bullet 5 subbullet 5 says that if int cannot represent all values ββin unsigned short, both operands are converted to unsigned int, and type
sum is unsigned int.
If the above analysis is legal, it means that using auto to declare variables initialized with arithmetic expressions on built-in types can lead to results determined by the implementation. I suppose this would surprise many programmers that the type sum above is not completely defined by the standard.
Is my reasoning legitimate?
c ++ c ++ 11
KnowItAllWannabe
source share