欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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());
相关标签: effective-c++