Say I am writing a enqueue() function that takes in a unique_ptr, but I only want to claim its ownership when enqueue returns success. If the queue is full I want to leave the unique_ptr intact (user can retry with the same item later)
bool enqueue(std::unique_ptr&& item){
if(!vector.full()){
vector.emplace(std::move(item));
return true;
}
return false;
}
// usage
auto item_ptr = make_unique<>();
while(!enqueue(std::move(item_ptr))){
// item_ptr is not moved
}
I can also define the function to take a lvalue reference instead
bool enqueue(std::unique_ptr& item)
while(!enqueue(item_ptr)){
// item_ptr is not moved
}
I am not sure which one to pick, they all seemed a little anti-patterned since usually std::move indicates the deletion of a unique_ptr (most of the time, I work with function that takes unique_ptr by value), maybe there's a better solution?