MISRA Discussion Forums
16.6.1 clarification - 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.16 Overloading (https://forum.misra.org.uk/forumdisplay.php?fid=202)
+---- Thread: 16.6.1 clarification (/showthread.php?tid=1733)



16.6.1 clarification - cgpzs - 26-03-2025

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!


RE: 16.6.1 clarification - misra cpp - 28-03-2025

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.


RE: 16.6.1 clarification - cgpzs - 31-03-2025

(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!