using c++ aggregate initialization in std::make_shared
Solution 1:
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.
Solution 2:
The inheritance solution feels a bit overkill to me, and adds a bit of readability confusion because the make_shared template is not your type.
Unaware readers might be confused about this aggregate_adapter part.
Here is an alternative:
#include <iostream>
struct Foo
{
std::string s;
int i;
char c;
};
template<typename T, typename... Args>
std::shared_ptr<T> MakeAggregateShared(Args&&... args)
{
return std::make_shared<T>(T{ std::forward<Args>(args)... });
}
int main(int argc, char* argv[])
{
auto foo = MakeAggregateShared<Foo>("hello", 5, 'c');
}