restrict qualifier on member functions (restrict this pointer)
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?
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?
I belive what you guys are missing is that an argument to a member function could also alias parts or an object. Here's an example
struct some_class {
int some_value;
void compute_something(int& result) {
result = 2*some_value;
...
result -= some_value;
}
}
One would probably expect that to compile to
*(this + offsetof(some_value)) -> register1
2*register1 -> register2
...
register2 - register1 -> result
That code, unfortunately, would be wrong if someone passes a reference to some_value for result. Thus, a compiler would actually need to generate to following
*(this + offsetof(some_value)) -> register1
2*register1 -> register2
register2 -> result
...
*(this + offsetof(some_value)) -> register1
result -> register2
register2 - register1 -> register2
register2 -> result
which is obviously way less efficient. Note that unless compute_something is inlines, the compiler has no way of knowing whether result may alias some_value or not, so it has to assume the worst case, no matter no smart or dumb it is. So there a definite and very real advantage to restrict, even if applied to the this pointer.
The link you posted is interesting. There will not be a solid use case for having restrict
applied on this
. As you mentioned in your question, copy constructor, operator = could have been potential candidates; but compiler can take care of them.
But following case can be interesting
struct A
{
//...
void Destroy (A*& p) __restrict__
{
delete this;
p = 0;
p++;
}
};
Now use case can be;
A **pp = new A*[10];
for(int i = 0; i < 10; i++)
pp[i] = new A;
//...
A* p = pp[0];
for(int i = 0; i < 10; i++)
p->Destroy(p);
delete[] pp;
Though this is very unusual practice, this is the only case I could think of this use case.
链接地址: http://www.djcxy.com/p/64046.html上一篇: 有没有办法避免严格的锯齿警告?
下一篇: 限制成员函数的限定符(限制这个指针)