This is too complicated. Why not just say the following:
int main() { Container baz { { 0, std::make_shared<MyClass>() } };
If you absolutely must go with an auxiliary function, you need to return the object, and not dangle the link. Something like that:
Container f() { return Container { { 0, std::make_shared<MyClass>() } }; }
It's hard to indulge anything more pedestrian than this, but one final, never-used home version:
Container f() { Container bar; auto p = std::make_shared<MyClass>; bar[0] = p;
source share