5-0-3: Meaning of "implicitly converted to a different underlying type"

Moderators: david ward, misra cpp

abgs
Posts: 12
Joined: Wed Sep 06, 2017 4:25 pm
Company: Gimpel Software

5-0-3: Meaning of "implicitly converted to a different underlying type"

Postby abgs » Mon Feb 11, 2019 4:00 pm

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 [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.

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

Re: 5-0-3: Meaning of "implicitly converted to a different underlying type"

Postby misra cpp » Wed Apr 10, 2019 2:48 pm

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)
Posted by and on behalf of
the MISRA C++ Working Group


Return to “6.5 Expressions (C++)”

Who is online

Users browsing this forum: No registered users and 1 guest