processWidget(std::shared_ptr<Widget>(new Widget), priority());
Yet this code can result in a memory leak.
At first, I thought about a weird interaction between
new, when the constructor of
Widget fails, but this case is handled
by the C++ runtime.
The first trick here is that function parameters evaluation order is unspecified,
new Widget might be called, then
priority(), then the value
new Widget is passed to
The second one is there since this is C++,
priority() might raise an exception,
new Widget will be called, but never passed to
std::shared_ptr, and thus never deleted!