不能只删除方法的 const 重载吗?
Can't delete only const overload of method?
至少对于一元 &
和一元 -
看来 GCC 只会让你删除运算符或 none 的非 const 和 const 版本(它可能会影响二元运算符,但我没有检查过)。如下评论所述,虽然我可以成功地基于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;
}
内置operator&
不参与重载决议([over.match.oper]/3.3)。
For the operator ,
, the unary operator &
, or the operator ->
, the built-in candidates set is empty.
假设您声明已删除下面的重载
const A* operator&() const = delete;
无论您是要获取 const
还是非 const
A
的地址,上面的声明都是唯一可行的候选者,导致编译错误。
如果你把它注释掉,那么内置的 operator&
是按照 [over.match.oper]/9.
找到的
If the operator is the operator ,
, the unary operator &
, or the operator ->
, and there are no viable functions, then the operator is assumed to be the built-in operator and interpreted according to Clause [expr].
现在,如果您将非const
重载声明为已删除
A* operator&() = delete;
这不能在 const A
对象上调用,因此它不是一个可行的候选者,并且会找到内置的 operator&
。
当处理重载 operator&
的 class 时,您可以使用 std::addressof
来获取实例的地址。
至少对于一元 &
和一元 -
看来 GCC 只会让你删除运算符或 none 的非 const 和 const 版本(它可能会影响二元运算符,但我没有检查过)。如下评论所述,虽然我可以成功地基于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;
}
内置operator&
不参与重载决议([over.match.oper]/3.3)。
For the
operator ,
, theunary operator &
, or theoperator ->
, the built-in candidates set is empty.
假设您声明已删除下面的重载
const A* operator&() const = delete;
无论您是要获取 const
还是非 const
A
的地址,上面的声明都是唯一可行的候选者,导致编译错误。
如果你把它注释掉,那么内置的 operator&
是按照 [over.match.oper]/9.
If the operator is the
operator ,
, theunary operator &
, or theoperator ->
, and there are no viable functions, then the operator is assumed to be the built-in operator and interpreted according to Clause [expr].
现在,如果您将非const
重载声明为已删除
A* operator&() = delete;
这不能在 const A
对象上调用,因此它不是一个可行的候选者,并且会找到内置的 operator&
。
当处理重载 operator&
的 class 时,您可以使用 std::addressof
来获取实例的地址。