3

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.

Danh
  • 5,916
  • 7
  • 30
  • 45
Henry Chen
  • 41
  • 1
  • 4
  • 2
    Can you explain or include a link to the definition of "cvalue" for those of us who aren't familiar with MISRA? – Brian Bi Dec 21 '16 at 06:19
  • Yeap, just like what I have said, the defintion of "cvalue" is `An expression that should not undergo further conversions, either implicitly or explicitly, is called a cvalue expression.`[misra 2008 compliance](https://bpb.opendns.com/a/www.ldra.com/attachments/article/74/MISRA-CPP_2008_COMPLIANCE.pdf?reg=short) – Henry Chen Dec 21 '16 at 06:32

1 Answers1

3

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.

Danh
  • 5,916
  • 7
  • 30
  • 45
  • Actually in C++ `signed char x=127, y=127; int z=x+y;` is perfectly defined to result in `z` being 254 in any implementation. – 6502 Dec 21 '16 at 07:29
  • I agree with you viewpoint. The same quote with you:In the end of page 57, MISRA Cpp 2008:` Binary expressions are not cvalues and their underlying type is the result of applying the underlying type conversions.` Here it says Binary expressions are not cvalues, whether this is conflicted with the `s32 = static_cast < int32_t > ( s8 ) + s8; // Example 2 - Compliant`.
    And if I chang the code like this:`s32 = (int32_t)(s8 + s8);` Should it be non-compliant.
    – Henry Chen Dec 21 '16 at 11:21
  • It's listed under additive section later – Danh Dec 21 '16 at 11:22
  • So do you consider that ` s32 = static_cast < int32_t > ( s8 ) + s8`,should be a cvalue expression belong to assignment operators "=" not an additive operators"+". – Henry Chen Dec 21 '16 at 11:52
  • 1
    cvalue by whatever constraint – Danh Dec 21 '16 at 12:13