lundi 7 octobre 2019

Why is this variable considered an lvalue? [duplicate]

This question already has an answer here:

Considering this code

class T {
public:
    T(T& x) = delete;
    T(T&& x) {}
};

void foo(T&& b) {
    T y(b);
}

I was expecting that b; which is an rvalue by declaration; and seemingly usage, should be passed into the move constructor of T in foo().

Instead; I get a compilation error reporting that T& has been deleted.

Replacing it with

void foo(T&& c) {
    T y(std::move(c));
}

Results in the expected success; but obviously one doesn't want to litter their code with std::move everywhere. As tempting as it is to blame visual studio - in this case I suspect it's my understanding that's wrong. Can someone please explain why move constructor isn't used?

Aucun commentaire:

Enregistrer un commentaire