Per my understanding, the following code constructs an object of type Foo and then moves that object into the memory allocated by std::make_shared
struct Foo { std::string s; int i; char c; }; int main(int argc, char* argv[]) { auto foo = std::make_shared<Foo>(Foo{"hello", 5, 'c' }); } Is it possible to aggregate initialize Foo directly into the memory allocated by std::make_shared?
An aggregate type is a structure, union, or array type. If an aggregate type contains members of aggregate types, the initialization rules apply recursively.
It constructs an object of type T passing args to its constructor, and returns an object of type shared_ptr that owns and stores a pointer to it.
So, if you throw exception from your class' constructor, then std::make_shared will throw it too. Besides exceptions thrown from constructor, std::make_shared could throw std::bad_alloc exception on its own.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With