MISRA Discussion Forums

Full Version: Clarification for underlying type of an integer constant expression
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
According to MISRA C++ 2008 section 6.5, the underlying type of an integer contant expression is calculated based on its evaluated value. Griven expressions as below:

1. unsgined long v1 = 1UL; //Underlying type of "1UL" is unsigned char.
2. if(1) {//...} //Underlying type if "1" is signed char.
3. if(2000-1999){//...} //Underlying type of expression "2000-1999" is signed char.

Are the undelying type of above expressions correct? Then for the above three cases, there should be implicit cases as following:
1. unsigned char => unsigned long
2. signed char => int
3. signed char => int

Correct?
Yes, your interpretation is correct (but examples 2 and 3 would break other rules - as the controlling expressions are not Boolean expressions)
Dear MISRA team,

is the underlying type of
Code:
static_cast(1U)

a) uint8_t (underlying type of an integer literal in chapter 6)?

or

b) uint16_t because the static_cast is taken into account (see static_cast(expression) in chapter 6)?

If the answer is a) then it would be impossible to make this line of code compliant with rule 5-0-10, right?

[code]const uint16_t MASK = 1U
The answer is b - its uint16_t Se p59