Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
16.6.1 clarification
#1
Hi,

Rule 16.6.1 marks this example as non-compliant:

Code:
C operator+( int32_t rhs ) const;  // Non-compliant

However it does not provide a compliant alternative. It's unclear if rule 16.6.1 wants us to:

a) Duplicate the function and create 2 versions for each order of the parameters, in order to guarantee symmetry (even if it may not be useful for the user, potentially being dead code):

Code:
C operator+(C       lhs, int32_t rhs );
C operator+(int32_t lhs, C       rhs );

b) Implement only 1 overload, but as a non-member function. This still does not solve the problem of "operator+ must be symmetrical", i.e that "1 + c" and "c + 1" must be supported.
Code:
friend C operator+(C lhs, int32_t rhs );

Could you clarify?

Thanks!
Reply
#2
The aim of this rule is not to force symmetry. As you point out, this may not be required.

The aim is to limit interactions between the intended behaviour and the possibility of implicit conversions, so either of your approaches may be appropriate, depending upon the required behaviour.
Posted by and on behalf of
the MISRA C++ Working Group
Reply
#3
(28-03-2025, 02:17 PM)misra cpp Wrote: The aim of this rule is not to force symmetry. As you point out, this may not be required.

The aim is to limit interactions between the intended behaviour and the possibility of implicit conversions, so either of your approaches may be appropriate, depending upon the required behaviour.

Understood, thanks for the quick response!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)