Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing unique_ptr to a function by reference

I noticed a behaviour in some library that I am compiling, which was slightly unexpected and wanted to clarify that.

There is a class which has a method of the following form:

void notify(Frame & frame);

Now, there is a caller which uses a unique_ptr as follows:

std::unique_ptr <Frame> localFrame (new Frame(rows, cols));

Now when it calls the method it does:

obj->notify(*localFrame);

So this relies on some implicit conversion of the underlying pointer to a reference.

My question is that is this cross platform and expected behaviour? Is there any use for me to do something something like:

obj->notify(*localFrame->get());
like image 721
Luca Avatar asked Sep 06 '25 16:09

Luca


1 Answers

"Some implicit conversion" is std::unique_ptr::operator* which is quite a standard operator that returns a reference to the pointed-to object. You don't need to overcomplicate it.

like image 186
bipll Avatar answered Sep 08 '25 18:09

bipll