首页 > 解决方案 > Effective Modern C++ 中的这个项目仍然是最新的吗?

问题描述

有效的现代 C++ 第 146 页:

void processWidget(std::shared_ptr<Widget> spw, int priority);
void cusDel(Widget *ptr);//a custom deleter

这是 C++17 之前的不安全调用:

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

它曾经是不安全的,因为可以在构造函数之后new Widget但之前调用computePriority std::share_ptr,如果computePriority产生异常,动态的allcoat Widget 将被泄露。因此你可以这样做:

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

现在这将在 shared_ptr 上添加一个复制构造函数操作。所以你也可以这样做:

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

所以我的问题是,以下代码是否仍然能够在 C++17 中泄漏内存?

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

我已经阅读了这个这个,但我仍然不确定,我相信std::shared_ptr<Wdiget>(new Widget, cusDel)并且computePriority()都在调用 processWidget 之前排序,但是我认为在获得新创建的对象的所有权之后和之前computePriority()仍然可以抛出异常。newshared_ptr

标签: c++c++17undefined-behaviorexception-safety

解决方案


C++17 did change the sequencing with regard to the evaluation of the expressions used to call a function. While it doesn't impose any particular order, it does say:

The initialization of a parameter, including every associated value computation and side effect, is indeterminately sequenced with respect to that of any other parameter.

"Indeterminately sequenced" is defined as:

Evaluations A and B are indeterminately sequenced when either A is sequenced before B or B is sequenced before A, but it is unspecified which.

So one parameter will be initialized before the other, including all side-effects. So either the first argument or the second argument is fully evaluated before the other.


推荐阅读