std::forward problem

Hello everyone,

I've been programming on C++ for a while but there are still many things I'm not aware of. I'd love some help on this particular issue. Im analysing some stuff and came up to this:

auto x = std::make_unique<int>(5);
auto y = std::forward<std::unique_ptr<int>&>(x); // compile-error
auto z = std::forward<std::unique_ptr<int>>(x); // no compile error - turns to nullptr

Although I get it to work, I can't manage to find how does the third line works to be honest. I've searched for a while but didn't find any info that could clear this doubt of mine.
std::forward<std::unique_ptr<int>&>(x); casts x to lvalue reference.

1
2
3
auto y = /*lvalue of type std::unique_ptr<int>*/;
//Same as
auto y = x;
— tries to invoke copy constructor of std::unique_ptr<int> which is deleted and cannot be used.

std::forward<std::unique_ptr<int>>(x); casts x to rvalue reference.


auto z = /*rvalue of type std::unique_ptr<int>*/; invokes move constructor of std::inique_ptr which moves data from x (making it null pointer) to z
I see! Alright then. Thank you very much for the quick reply.
Topic archived. No new replies allowed.