Why is this an issue?
This is a draft version of a MISRA C++ 202x rule proposed for public review.
MISRA Rule 16.5.2
Analysis Type: Decidable,Single Translation Unit
Taking the address of an object of incomplete type where the complete type contains a user-declared
operator& results in
undefined behaviour (until C++11) or unspecified behaviour (since C++11).
& operator can make code harder to understand as
*&a may not give the same result as
std::addressof will always return the address of an object without there being a risk of undefined or
A * operator&(); // Non-compliant
void f1( A & a )
&a; // Undefined or unspecified behaviour
void f2( A & a )
&a; // Uses user-defined operator&
Copyright The MISRA Consortium Limited © 2023
S877 targets the same defect as this rule but for a non-mission-critical context.
External coding guidelines
Core Guidelines C.166- Overload unary & only as part of a system of smart pointers and references