Effective C++ 17. Store newed objects in smart pointers in standalone statements
程序员文章站
2022-03-11 17:26:35
...
int priority();
void processWidget(std::tr1::shared_ptr<Widget> pw, int priority);
// it won't compile, tr1::shared_ptr constructor
// taking a raw pointer is explicit
processWidget(new Widget, priority());
processWidget(std::tr1::shared_ptr<Widget>(new Widget), priority());
the order of performing maybe
1. Execute “new Widget”.
2. Call priority.
3. Call the tr1::shared_ptr constructor.
If the call to priority yields an exception, the pointer returned from “new Widget” will be lost.
// Store newed objects in smart pointers in standalone statements
std::tr1::shared_ptr<Widget> pw(new Widget);
processWidget(pw, priority());
上一篇: basename & dirname