Is it a bad usage to pass reference of a unique_ptr to the function?
void Foo(std::unique_ptr<T>& rT)
{
...
}
Is it a bad usage to pass reference of a unique_ptr to the function?
void Foo(std::unique_ptr<T>& rT)
{
...
}
void Foo(std::unique_ptr<T>& rT)
implies two things:
You cannot call the function like this: Foo(std::unique_ptr<T>(new T));
since it is a non-const lvalue reference. This restricts your function in only accepting lvalues.
If you pass an lvalue, you have no idea what happens to it: Foo(myunique_ptr);
. In a call like this, you have no idea if Foo
claims ownership of the std::unique_ptr
. This can be dangerous if the caller is unaware of what happens inside of Foo
and it is definitely not clear from the function signature.
Now after taking the above into consideration, you can decide whether it is bad usage or not.