You could create an adapter with a variadic constructor template to forward the arguments, something like:
template<class T>
struct aggregate_adapter : public T {
template<class... Args>
aggregate_adapter(Args&&... args) : T{ std::forward<Args>(args)... } {}
};
And then you can do:
auto foo = std::make_shared<aggregate_adapter<Foo>>("hello", 5, 'c');
Since aggregate_adapter<Foo>
and Foo
are related, foo
is convertible to std::shared_ptr<Foo>
as well.
Caveats
Unfortunately, the use of forwarding also makes it impossible to brace-init any of the members like std::make_shared<aggregate_adapter<Foo>>({'h','e','l','l','o'}, 5, 'c');
without specifying the type explicitly, but the same restriction applies to make_shared already.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…