限制成员函数的限定符(限制这个指针) [英] restrict qualifier on member functions (restrict this pointer)
问题描述
restrict
关键字,而是关于将其应用于成员函数如此处所述。 gcc允许您使用 __ restrict __
(相当于C99的 restrict
)限定符的GNU ++在一个成员函数上,有效地使这个
成为函数范围内的一个限制限定指针。牛肉在哪里?
大多数成员函数在其他成员上工作,通过这个
访问它们,这是一个 T * const
(并且通常是不确定的)。对于这个
可能是别名,需要在成员函数中使用第二个指向类型的指针,并且它必须来自某处。
对于非成员函数来说,这是一种情况,例如所有二元运算符或任何其他至少需要两个指针或引用相同的非平凡类型的自由函数。然而,这些函数没有这个
,所以它们不相关。
赋值运算符,复制构造函数和一元比较运算符是成员函数的示例,其中 this
could 原则上可以是别名(因为另一个对象是通过引用传递的)。因此,为这些分配限制限定符才有意义 - 编译器应该已经很清楚所有其他函数都具有restrict属性(因为从来没有第二个指向T的指针)。
现在,如果您在 那么,如果真的想给成员函数一个限定符限定符并且它有意义,那会是什么情况呢? 要么我错过了一些东西,要么你的问题没有意义。 关于将它应用到赋值运算符,您正确地指出它会避免需要显式自我更新分配测试。然后你说: 很显然,这是你事先不可能知道的东西。 但是,对任何东西使用 I我也不确定为什么你发现成员函数不可能将指针(或引用)指向另一个相同类型的对象。简单的例子: 这种东西会一直冒出来。 所以真正的问题是,为什么你认为 Note: To clarify, the question is not about the use of the gcc allows you to use the Most member functions work on other members, accessing them via The assignment operator, copy constructor, and unary comparison operators are examples of member functions where Now, if for example you used 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. 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 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: This sort of thing crops up all the time. So the real question is, why do you think 这篇关于限制成员函数的限定符(限制这个指针)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! operator =
上使用了 restrict ,那么您应该 根本不检查自赋值,因为你认为
这个
不在该函数的范围内(和 >如果这是真的,则不会发生自我分配)。
显然,这是事先不可能知道的事情,也是不合理的。 / p>
这个
不是那个 与成员函数的任何其他参数不同,那么为什么你会惊讶GCC允许你应用限制
到它?
restrict
时总是 em 。例如,有人可能会决定调用带有重叠内存区域的 memcpy
;你不可能事先知道他们不会这样做。但 memcpy
参数的 restrict
声明意味着它们提交了一个错误,如果它们。以完全相同的方式,如果您声明了赋值运算符 restrict
,那么您为某人自行分配该类的对象造成了错误。这一点根本没有什么神秘或矛盾的;它只是 restrict
语义的一部分,它对你的代码的其余部分施加了一定的限制。
class Point {
public:
double distance(const Point& other)const ;
};
这个
与其他参数是如此不同?或者,如果你愿意,我怎么完全错过你的观点?restrict
keyword in general, but specifically about applying it to member functions as described here.__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?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.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).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.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?
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.class Point {
public:
double distance(const Point &other) const;
};
this
is so different from any other argument? Or if you prefer, how did I miss your point so completely?