Reputation: 258628
Are there any special rules that apply to the unary & operator?
For example, the code:
#include <iostream>
struct X
{
X() {}
void* operator &() { return NULL; }
};
int main()
{
const X x;
std::cout << &x << std::endl;
X y;
std::cout << &y;
}
produces the output
0xbfbccb33
0
I knew this would compile and run like this because of a discussion I've had here before, but hadn't I known this, I would have expected this to fail to compile, because operator &
is not declared const
.
So it appears that the compiler generates operator &() const
regardless of whether operator &()
is overloaded or not. Fine, this makes sense, especially with the sample and output.
The question is where is this behavior detailed in the standard?
I'm not looking for answers that re-iterate what I already stated in the question, so please don't explain how my overloaded operator can't be called on a const
object, because I already know that.
Upvotes: 20
Views: 403
Reputation: 55897
n3337 13.3.1.2/9
If the operator is the operator ,, the unary operator &, or the operator ->, and there are no viable functions, then the operator is assumed to be the built-in operator and interpreted according to Clause 5.
Upvotes: 14