I’m trying to understand the Cvalue concept in MISRA C++ 2008. According to the documentation, a Cvalue expression is one that shouldn’t go through any more conversions, whether implicit or explicit.
However, when I look at the code examples that are marked as compliant:
int32_t result = static_cast<int32_t>(byte_val) + byte_val; // Compliant example
result = result + byte_val; // Also compliant
I can see that the right side operands in these addition operations are clearly undergoing implicit and explicit type conversions. Since the rule says these examples are compliant, it seems to contradict the Cvalue definition. Can someone explain how this works? Am I missing something about when the Cvalue concept applies?
cvalue means the expression hit its final type and you shouldn’t mess with it anymore. Your examples work because those conversions happen during evaluation, not after. Think of it like cooking - you mix ingredients (conversions) to make the dish (final expression), but once it’s done you don’t keep throwing random stuff in.
You’re overthinking the terminology. The Cvalue concept doesn’t ban all conversions - it stops unnecessary ones that break type safety or cause weird behavior. When MISRA says “shouldn’t go through any more conversions,” they mean conversions beyond what the operation actually needs. Your compliant examples? Those conversions are just standard arithmetic promotions that C++ requires for addition to work. They’re not extra or dangerous - they’re the bare minimum needed. The rule targets redundant casting or implicit conversions that could lose data or give unpredictable results. Your byte_val getting promoted during arithmetic is normal behavior, not a Cvalue violation.
The confusion comes from not understanding when Cvalue actually applies. A Cvalue expression is the final result after all conversions are done - not the individual pieces inside the expression. In your examples, operands like byte_val still go through normal arithmetic promotions just like they’re supposed to. Cvalue only kicks in for the entire expression once it’s in its final state where no more conversions should happen. Think of it as the expression’s “final form” rather than a rule that blocks intermediate conversions during evaluation. The rule basically says once you’ve got a properly converted result, don’t do extra unnecessary conversions on that final value. That’s why your examples work fine - those conversions are the standard ones needed to make the arithmetic operations actually work.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.