MISRA Discussion Forums
5-0-3: Meaning of "implicitly converted to a different underlying type" - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19)
+---- Forum: 6.5 Expressions (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=134)
+---- Thread: 5-0-3: Meaning of "implicitly converted to a different underlying type" (/showthread.php?tid=1469)



5-0-3: Meaning of "implicitly converted to a different underlying type" - abgs - 11-02-2019

Rule 5-0-3 says "A cvalue expression shall not be implicitly converted to a different underlying type."

The rationale goes on to say "an expression defined as a cvalue shall not undergo further implicit conversions".

In the expression 's64 = s32 + s32', the addition is a cvalue expression of underlying type int32_t. It is being assigned to an object of type int64_t. The C++ standard says (regarding the assignment operator): "If the left operand is not of class type, the [align=right] expression is implicitly converted to the cv-unqualified type of the left operand".

Does this "implicit conversion to the type of the left operand" qualify as causing the cvalue expression to be "implicitly converted to a different underlying type"?


In the expression 's64 == s32 + s32' the addition is a cvalue expression of underlying type int32_t. The C++ standard says (regarding the equality operator): "The usual arithmetic conversions are performed on operands of arithmetic or enumeration type", the result of which will be that "the
operand with the type of lesser integer conversion rank shall be converted to the type of the operand with greater rank". Additionally, MISRA C++ defines "underlying type conversions" and says they are "conceptual replacements for the usual arithmetic conversions". In this case, the "underlying type conversions" say: "... if both operands have integral type, the underlying type of the expression can be found using the following: ... the type of the result is that of the larger type". Whether the "usual arithmetic conversions" or the "underlying type conversions" are used, the result of the addition will be converted to int64_t.

Does the application of the usual arithmetic conversions and the ensuing implicit conversion to the larger type qualify as causing the cvalue expression to be "implicitly converted to a different underlying type"?


In the expression '0L == s32 + s32' (where long is int64_t and 0L is signed char as established here) does the implicit conversion from the promotion of the addition expression to int64_t qualify as causing the cvalue expression to be "implicitly converted to a different underlying type"?


Note that these examples intend to use s32 and 64 to avoid the operands to the addition operator undergoing sub-int promotion, i.e. s32 is 32-bit int and s64 is 64-bit long.


Re: 5-0-3: Meaning of "implicitly converted to a different underlying type" - misra cpp - 10-04-2019

The purpose of the underlying type rules is to remove the dependency on 'usual arithmethic conversions' and other asymmetrical type behaviours defined in the standard, and to impose a uniform interpretation of arithmetic. Hence s64 = s32 + s32; is non-compliant by analogy with Example 1

According to the current rules, 0L == s32+s32 is compliant (0L is signed char, not a cvalue and can be converted to s32)