Search code examples
c++overloadingdelete-operatorrvalue

Deleting all rvalue function overloads of a class


Say I have a class object that must be captured by the caller when returning this class's object from a function call.

// no_can_rvalue *must* be captured
[[nodiscard]] no_can_rvalue a_func();

I can enforce this by deleting all rvalue function overloads, thus making it impossible to use the class functionality unless a caller has captured an object of it (doubled with nodiscard in c++17).

Is it possible to delete all rvalue function overloads of a given class in one fell swoop?

The result being equivalent to :

struct no_can_rvalue {
    void f() && = delete;
    void f() &;
    void g() && = delete;
    void g() &;
    // etc
};

Solution

  • No, it is not possible to do so.