假设某些类型 Foo
有一个重载的 operator->
返回一个 Bar*
:
struct Foo
{
Bar* operator->();
};
如果我想从 Foo
类中就地析构返回的 Bar
实例,我可以编写以下代码吗?
this->~Bar();
g++ 不喜欢那个代码。如果我这样写就可以了:
(*this)->~Bar();
“递归转发规则”在这种情况下不适用吗?为什么不呢?
最佳答案
这是链接 ->
的规则,可在标准的 13.5.6 [over.ref]
中找到:
An expression
x->m
is interpreted as(x.operator->())->m
for a class objectx
of typeT
ifT::operator->()
exists and if the operator is selected as the best match function by the overload resolution mechanism (13.3).
因为 this
是一个指针,而不是一个类对象,所以它不适用。
相反,5.2.5 ([expr.ref]
) 中的这条规则适用:
For the second option (arrow) the first expression shall have pointer to complete class type. The expression
E1->E2
is converted to the equivalent form(*(E1)).E2
; the remainder of 5.2.5 will address only the first option (dot).
关于c++ - 对具有重载运算符的类型进行就地销毁->,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6996600/