Note: To clarify, the question is not about the use of the restrict
keyword in general, but specifically about applying it to member functions as described here.
gcc allows you to use the __restrict__
(the GNU++ equivalent to C99's restrict
) qualifier on a member function, effectively making this
a restrict qualified pointer within the function's scope. Where is the beef?
Most member functions work on other members, accessing them via this
, which is a T* const
(and normally unaliased). For this
to be possibly aliased, there would need to be a second pointer-to-type in use within the member function somehow, and it would have to come from somewhere.
That is a situation which is regularly the case with non-member functions, such as for example all binary operators or any other free function that takes at least two pointers or references of an identical, non-trivial type. However, these functions don't have a this
, so they're not relevant.
The assignment operator, copy constructor, and unary comparison operators are examples of member functions where this
could in principle be aliased (since another object is passed via reference). So it only really makes sense to assign a restrict qualifier to these -- it should already be obvious to the compiler that all other functions have the restrict property anyway (because there is never a second pointer-to-T).
Now, if for example you used restrict
on operator=
you should consequentially not check for self-assignment at all, because you're saying that this
is not aliased within the scope of that function (and if that's true, no self-assignment can possibly happen).
Obviously, this is something that you cannot possibly know in advance, and it's something that doesn't make sense either.
So, what would be a case where one would actually want to give a member function a restrict qualifier and where it makes sense?