Clarification for underlying type of an integer constant expression

Moderators: david ward, misra cpp

Post Reply
huangcheng_synopsys
Posts: 1
Joined: Sat Apr 01, 2017 4:39 am
Company: Synopsys

Clarification for underlying type of an integer constant expression

Post by huangcheng_synopsys » Fri Apr 14, 2017 3:08 am

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?

misra cpp
Posts: 148
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Clarification for underlying type of an integer constant expression

Post by misra cpp » Mon Jul 10, 2017 11:12 am

Yes, your interpretation is correct (but examples 2 and 3 would break other rules - as the controlling expressions are not Boolean expressions)
Posted by and on behalf of
the MISRA C++ Working Group

dg1980
Posts: 109
Joined: Wed Apr 27, 2016 2:33 pm
Company: Elektrobit Automotive GmbH

Re: Clarification for underlying type of an integer constant expression

Post by dg1980 » Thu Sep 14, 2017 6:18 am

Dear MISRA team,

is the underlying type of

Code: Select all

static_cast<uint16_t>(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<type-id>(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: Select all

const uint16_t MASK = 1U << 10U;// casting to underlying type of uint8_t as requested per 5-0-10 would result in MASK=0
Please clarify.
Thanks

misra cpp
Posts: 148
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Clarification for underlying type of an integer constant expression

Post by misra cpp » Fri Sep 22, 2017 4:19 pm

The answer is b - its uint16_t Se p59
Posted by and on behalf of
the MISRA C++ Working Group

Post Reply

Return to “6.5 Expressions (C++)”