Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 6-2-1 Assignment operators shall not be used in subexpressions
#3
Firstly, we agree with nishiyama’s interpretation of 6-2-1, the first example is compliant and the second isn’t. The technical reason for why the first is compliant is that it doesn’t contain an assignment (as defined by the C++ standard). When an object is declared, what appears to be an assignment is an initialisation. The C++ standard defines different behaviours for assignment and initialisation (e.g. you cannot assign to a const object, but you can – indeed must – initialise it), so MISRA C++ makes the same distinction.

The more practical reason is that there is no reason to ban if ( int16_t i = foo ( ) ) as there is no possibility of unexpected or undefined behaviour and it would be difficult to ban without also banning for ( int16_t i = 0; … which is such a common coding idiom that it has to be allowed.
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 3 Guest(s)