Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 7.0.6 - why the requirement of "id-expression"?
#1
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!
Reply
#2
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")
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Forum Jump:


Users browsing this thread: 6 Guest(s)