It might be more efficient to have a make_foo
helper:
Foo make_foo() { return Foo(std::make_shared<Stuff>()); }
Now you can say auto f = make_foo();
. Or at the very least use the make_shared
invocation yourself, since the resulting shared_ptr
may be more efficient than the one constructed from a new
expression. And if Stuff
actually takes constructor arguments, a private auxiliary constructor might be suitable:
struct Foo
{
template <typename ...Args>
static Foo make(Args &&... args)
{
return Foo(direct_construct(), std::forward<Args>(args)...);
};
private:
struct direct_construct{};
template <typeaname ...Args>
Foo(direct_construct, Args &&... args)
: m_myStuff(std::make_shared<Stuff>(std::forward<Args>(args)...)) // #1
{ }
};
You can either wrap Foo::make
into the above make_foo
, or use it directly:
auto f = Foo::make(true, 'x', Blue);
That said, unless you're really sharing ownership, a std::unique_ptr<Stuff>
sounds like more preferable approach: It is both conceptually much simpler and also somewhat more efficient. In that case you'd say m_myStuff(new Stuff(std::forward<Args>(args)...))
in the line marked #1
.