In MISRA C++ 2008, anyone knows the specialized notion Cvalue expression occured in the rule 5-0-3?

2.9k views Asked by At

From the concept of the Cvalue, I realized that

"An expression that should not undergo further conversions, either implicitly or explicitly, is called a cvalue expression."

But with the example presented by this rule.

s32 = static_cast < int32_t > ( s8 ) + s8; // Example 2 - Compliant
s32 = s32 + s8; // Example 3 - Compliant

Obviously, the right addition expressions occur implicitly and explicitly conversions here. And this rule mark them as compliant. I think it is conflicted with the notion cvalue.

1

There are 1 answers

5
Danh On BEST ANSWER

In the end of page 57, MISRA Cpp 2008:

Similar, unless listed below:

  • . . .

  • The other unlisted expressions are not cvalues and have the underlying type of the operation.

Reading the long list after that paragraph, nothing can be applied to s8.

Then, s8 is not cvalues, it has the underlying type of the operation, in your example, its underlying type is int32_t. The promotion to int32_t doesn't violate the rule.


The whole point of 5-0-3 is it wants to ensure all operations are performed in the same underlying type.

int32_t s32;
int8_t s8;
s32 = static_cast < int32_t > ( s8 ) + s8; // Example 2 - Compliant
s32 = s32 + s8; // Example 3 - Compliant

In those above examples, the + is performed with the underlying type of int32_t (by now, most of int is int32_t or int16_t), return the underlying type of int32_t, then assign to an int32_t variable, thus compliant with MISRA Cpp.

In this example:

int32_t s32;
int8_t s8;
s32 = s8 + s8;

It doesn't compliance because the additive operator is performed with the type of int, the result will be converted to int32_t which is not necessary the same with int, thus not-compliance.