Note: Changing the return type of FoobarFactory is also acceptable, but I cannot use forced copy "elision" (prvalue) due to needing to initialize things after construction of the initial Foobar object. The type Foobar is not copy-constructable, nor copy-assignable.
For completeness, here is FoobarFactory():
1 2 3 4 5 6 7 8 9
std::unique_ptr<Foobar> FoobarFactory() {
// lots of initialization goes here [...]
auto pFoobar = std::make_unique<Foobar>( [...] );
pFoobar->InitializeForReals();
return pFoobar;
}
I can't think of any other good way to extend the lifetime of the unique_ptr. I thought of a static variable, but that destroys the RAII-destructability, thread safety, etc.