vendredi 24 mai 2019

Overloading for rvalue? [duplicate]

This question already has an answer here:

Below is a code demonstrating the question:

class X {};
X x;
X&& rvalue_ref = std::move(x);
static_assert(std::is_same<decltype(rvalue_ref), X&&>::value, "Different types"); //To be sure that type is X&&
void func(X&) {
    cout << "lvalue reference";
}

void func(X&&) {
    cout << "rvalue reference";
}
int main() {
    func(rvalue_ref);
}

The output:

lvalue reference

Could you please explain the reason for that? We have a variable of the type X&& and an overload for this type but this overload isn't called.

Aucun commentaire:

Enregistrer un commentaire