Jul 14, 2014

[C++11] order of evaluation of arguments does matter

argument evaluation order is undefined in C++.
(Python is always from left to right)

This has impact on resource management.



processWidget(std::shared_ptr<Widget>(new Widget), // potential
computePriority()); // resource leak!

processWidget(std::make_shared<Widget>(), // no potential
computePriority()); // resource leak


However, if we need customize deleter,
make the shared_ptr a standalone expression.


processWidget( // as before,
std::shared_ptr<Widget>(new Widget, cusDel), // potential
computePriority() // resource
); // leak!

std::shared_ptr<Widget> spw(new Widget, cusDel);
processWidget(spw, computePriority());

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.