2

我有一个类A,它有一个不能在临时对象上调用的非常量成员函数foo,这就是它的样子:

struct A
{
    void foo() &
    {...}
};

如果我必须显式删除版本foo,因为 const rvalues 可以绑定到 const lvalues,实际上下面的代码编译得很好constconst&&

struct A
{
    void const_foo() const &
    {...}

    // void const_foo() const&& = delete;
    // ^^^^ uncomment to prevent calling foo on const rvalues
};

int main()
{
    const A a;
    std::move(a).const_foo();
}

所以有理由显式删除一个 const rvalue ref 限定函数,非 const 函数也有什么原因吗?

4

0 回答 0