Consider I have a container std::map<int, std::shared_ptr<MyClass>> and I want to fill it in external function and avoid coping of its contents. So I have
typedef Container std::map<int, std::shared_ptr<MyClass>>
Container&& f(){
Container bar;
auto foo = std::shared_ptr<MyClass>(new MyClass());
bar.insert(std::make_pair(0,foo));
std::cout<<bar.at(1)->print_smth<<'\n'; //This works
return std::move(bar);
}
int main(){
Container baz(f());
std::cout<<bar.at(1)->print_smth<<'\n'; //This doesn't
// Container baz has element 1, but shared_ptr is invalidated, because it has 0 references.
}
If I use conventional copy constructor everything works as expected.
This is far too complicated. Why not just say this:
If you absolutely must go with the helper function, you have to return an object, not a dangling reference. Something like this:
It’s hard to indulge anything more pedestrian than this, but one final, never-to-be-used-at-home version: