Should I explicitly =delete non-const rvalue ref qualified function?

74 views Asked by At

I have a class A that has a non-const member function foo that must not be called on temporaries, this is how it looks like:

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

If foo were const I would have to explicitly delete the const&& version since const rvalues can bind to const lvalues, in fact the following code compiles just fine

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();
}

So there's a reason to explicitly delete a const rvalue ref qualified function, are there any reasons also for non-const functions?

0

There are 0 answers