MISRA Discussion Forums
Rule 7.0.6 - why the requirement of "id-expression"? - 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++:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=188)
+---- Forum: 4.7 Standard conversions (https://forum.misra.org.uk/forumdisplay.php?fid=193)
+---- Thread: Rule 7.0.6 - why the requirement of "id-expression"? (/showthread.php?tid=1720)



Rule 7.0.6 - why the requirement of "id-expression"? - cgpzs - 09-01-2025

Rule 7.0.6 provides the following amplification for assigning numerical types:

2. The source and target shall have types of the same type category, signedness, the source size
shall be smaller than the target size, and the source shall be an id-expression;

Question: why must the source be an "id-expression"?

Consider this example:

Code:
struct Foo
{
  float bar;
};

float a;
Foo f;

double d1 = a;      // Compliant, since it's an id-expression
double d2 = f.bar;  // Non-compliant, since it's NOT an id-expression (it's a member-expression)

This is inconsistent. The fact that we are using a member-expression instead of an id-expression does not have any impact on how the conversion is made, it's equally safe. 

Would it make sense to remove the "id-expression" part of the rule? Otherwise, can you elaborate on why is it needed, what problems does it guard against?

Thanks!


RE: Rule 7.0.6 - why the requirement of "id-expression"? - misra cpp - 10-01-2025

The argument for 'id-expression' is that we wanted to keep the rule simple, whilst preventing i1 = s1 + s2; (a widening assignment).
We tried to enumerate the possible cases which were safe, but this got very complex, so we went to a simple case that was safe.

We will consider the rule in future to allow other safe cases, such as your example of class member access ("member-expression")