consistency - conventional
Why is this an issue?
Taking the address of an object of incomplete type, where the complete type contains a user declared operator &
leads to undefined
behavior.
Noncompliant code example
// A.h
class A
{
public:
A * operator & ( ); // Noncompliant
};
// f1.cc
class A;
void f ( A & a )
{
&a; // uses built-in operator &
}
// f2.cc
#include "A.h"
void f2 ( A & a )
{
&a; // use user-defined operator &
}
Resources
- MISRA C++ 2008, 5-3-3 - The unary & operator shall not be overloaded.
- C++ Core Guidelines C.166 - Overload
unary
&
only as part of a system of smart pointers and references