Note: To clarify, the question is not about the use of the restrict
keyword in general, but specifically about applying it to member functions
Either I am missing something, or your question does not make sense. this
is not that different from any other argument to a member function, so why are you surprised that GCC allows you to apply restrict
to it?
Regarding applying it to an assignment operator, you rightly point out that it would obviate the need for an explicit self-assignment test. Then you say:
Obviously, this is something that you cannot possibly know in advance
But this is always true when you use restrict
for anything. For example, somebody might decide to call memcpy
with overlapping memory regions; you "cannot possibly know in advance" that they will not do so. But the restrict
declaration for the arguments of memcpy
means they have committed an error if they do. In exactly the same way, if you declare an assignment operator restrict
, you have made it an error for someone to self-assign objects of that class. There is nothing mysterious or contradictory about this at all; it is just part of the semantics of restrict
that it imposes certain constraints on the rest of your code.
I am also not sure why you find it so impossible for a member function to take a pointer (or reference) to another object of the same type. Trivial example:
class Point {
public:
double distance(const Point &other) const;
};
This sort of thing crops up all the time.
So the real question is, why do you think this
is so different from any other argument? Or if you prefer, how did I miss your point so completely?