You have three options:
-
Give up ownership. This will leave your local variable without access to the dynamic object after the function call; the object has been transferred to the callee:
f(std::move(derived)); -
Change the signature of
f:void f(std::unique_ptr<Derived> const &); -
Change the type of your variable:
std::unique_ptr<base> derived = std::unique_ptr<Derived>(new Derived);Or of course just:
std::unique_ptr<base> derived(new Derived);Or even:
std::unique_ptr<base> derived = std::make_unique<Derived>(); -
Update: Or, as recommended in the comments, don’t transfer ownership at all:
void f(Base & b); f(*derived);