Effective C++ 17. Store newed objects in smart pointers in standalone statements

来源:互联网 发布:软件 原型 设计 工具 编辑:程序博客网 时间:2024/06/14 16:28
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 explicitprocessWidget(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 statementsstd::tr1::shared_ptr<Widget> pw(new Widget);processWidget(pw, priority());