0

I was trying to pass in a unique pointer to a setter and the compiler was complaining that on "x = newval;" the = operator was deleted. When calling std::move(newval) it works fine. But it seems to me that newval should already have been an rvalue reference type. Whats going on?

class A {
    public:
    A(int* i) : x(i) {}
    std::unique_ptr<int> x;
    void set_ptr(std::unique_ptr<int>&& newval) { x = newval; }
};

See in Compiler Explorer

Why does the compiler think that newval is not an rvalue reference type?

Bonus Question: Would it be better to pass the unique_ptr by value such that a copy is move constructed?

Thanks!

0 Answers0