I was just browsing through the draft of the C++11 standard and found the following puzzling statement (§13.6/8):
For every type
T
there exist candidate operator functions of the formT* operator+(T*);
How should this "unary +" operator on pointer be understood? Is this just a no-op in the normal case, which can nevertheless be overloaded? Or is there some deeper point I am missing here?
Well, you could overload it do do whatever you want, but it's just there for symmetry with the unary - operator. As you mention, it's just a no-op most of the time.
The
+
on pointers is a noop except for turning things to rvalues. It sometimes is handy if you want to decay arrays or functionsNow
x
is anint*&&
and not anint(&)[3]
. If you want to passx
or+a
to templates, this difference might become important.a + 0
is not always equivalent, considerThe last line is ill-formed, because adding anything to a pointer requires the pointer's pointed-to class type to be completely defined (because it advances by
sizeof(forward_decl) * N
bytes).The answer to your question is just a page above the quote you cited — §13.6/1: