lundi 10 octobre 2022

How can rvalue reference bind to lvalue during function call

AFAI understand, rvalue reference cannot bind to lvalue. But going through https://en.cppreference.com/w/cpp/utility/forward I see an example code section has:

template<class T, class U>
std::unique_ptr<T> make_unique1(U&& u)
{
    return std::unique_ptr<T>(new T(std::forward<U>(u)));
}

which is called using:

int i = 1;
auto p2 = make_unique1<A>(i); // lvalue

Why/how does this work? i being lvalue shouldn't bind to U&& unless we explicitly call std::move(i), right? Or I am missing something here.

Aucun commentaire:

Enregistrer un commentaire