【发布时间】:2017-05-02 19:47:15
【问题描述】:
至少对于一元 & 和一元 - 来说,GCC 似乎只会让您删除运算符的非常量和常量版本,或者不删除(它可能会影响二元运算符,但我没有检查过)。如下面的 cmets 所述,虽然我可以基于 const 成功重载,但我无法单独删除 const 或非 const 重载而不会遇到编译错误。此行为标准是否符合?这似乎违反直觉。
使用 GCC 5.4.0 测试。
#include <iostream>
struct A {
// These both being defined at the same time is fine,
// and whether or not x is const as expected will change
// which overload you get.
A* operator&() {
std::cout << "hello" << std::endl;
return this;
}
const A* operator&() const {
std::cout << "world" << std::endl;
return this;
}
// assuming both definitions above are commented out,
// regardless of whether or not x is const
// either one of these lines being present
// will make the example not compile!
// A* operator&() = delete;
// const A* operator&() const = delete;
// Finally if you have the const version defined and the non-const version deleted
// or vice versa, it will compile as long as the one that you have defined
// matches the constness of x.
};
int main(int argc, char** argv)
{
A x;
std::cout << &x << std::endl;
return 0;
}
【问题讨论】:
-
可以在非
const对象上调用const成员函数。我不确定你在期待什么。 -
@T.C.可以,但是即使不调用它也会发生错误(当 x 是非常量并且仅删除 const 版本时)。它也发生在相反的情况下(const x 仅删除了非常量版本)。我试图说明这一点,但这确实很棘手。
-
在上面的代码中,如果您注释
const A* operator&() const { ... }并取消注释const A* operator&() const = delete;,它将编译为非constA对象。同样,您可以通过注释/取消注释其他组合来编译const A,那么您的问题是什么?而不是代码中的所有这些 cmets,也许尝试发布所有不会编译的版本。 -
@Praetorian 我试图解释这将在第三个评论块中起作用。尝试按照第二个评论块中的说明进行操作:coliru.stacked-crooked.com/a/8ebec6956aae7ace
-
正如 TC 已经说过的,
const成员函数可以在非const对象上调用,并且删除的函数是您示例中唯一可用的候选函数,因此它当然不会编译。您可以创建对象const,但由于同样的原因它仍然无法编译。
标签: c++ c++11 operator-overloading overloading