Consider this as another tool in the toolbar:
bool Add(std::unique_ptr<Item>&& item);
This combines the advantages of path 2 and path 3. Ie it will only accept the rvalue value unique_ptr(for example, 2), but if there is some error when adding it to the container, it can save ownership as 3. Something like: can be used:
void
foo(MyContainer& c)
{
std::unique_ptr<Item> p = get_Item();
try
{
c.Add(std::move(p));
}
catch (...)
{
log_error_wtih(*p);
}
}
source
share