Safely interfacing with C-style factories
Consider a factory function which creates a widget at the end of a double pointer:
void create_widget(Widget** ptr);Being a good C++ developer, you want to manage your Widgets with smart pointers. This means that you need to do a little dance to set up the ownership:
std::unique_ptr<Widget> ptr();// Needs a separate variable otherwise we try to take the address of an rvalueWidget* raw_ptr = nullptr;create_widget(&raw_ptr);ptr.reset(raw_ptr); // Forget this and we leakC++23 gives you a better way to do this, with std::out_ptr.