lundi 28 août 2017

Is the unique_ptr returned from a function an lvalue?

Lately, I've been doing a little digging into the C++11 std and was playing around with unique_ptrs.

Let's say I have a function which returns a unique_ptr to an integer.

unique_ptr<int> GetUniquePtr(int i)
{
   return make_unique<int>(i);
}

In my main function, I am able to take the address of the value returned by the function. This means that the expression must evaluate to an lvalue

int main()
{
   cout << &(GetUniquePtr(5));
}

I know if I assign the function call to a unique pointer, the move consturctor of the unique_ptr will be called, treating the returned value as an rvalue reference.

int main()
{
   unique_ptr<int> uPtr = GetUniquePtr(5);
}

This dual behaviour of returning unique_ptr kind of confuses me, as I was of the impression that unique_ptr returned from a function call is always evaluated as an rvalue.

Can anyone shed some light on what's actually going on?

Aucun commentaire:

Enregistrer un commentaire