我的印象是下面容易泄露
class Something {
std::unique_ptr<A> a;
std::unique_ptr<int> b{new int{3}};
std::unique_ptr<C> c;
public:
Something() : a{new A{}}, c{new C{}} {};
};
如果评估顺序如下
新 A{}
新整数{3}
新 C{}
一个{}
b{}
c{}
我正在查看新的 C++17 功能 ( http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0145r3.pdf ),它修复了以前版本的 C++ 中的求值顺序错误,但它似乎只讨论了函数参数求值。
我查看了关于评估顺序的 cppreference 文档 (http://en.cppreference.com/w/cpp/language/eval_order),它似乎也没有提到这一点。
以上是否有明确定义的评估顺序? (现在在 C++17 或以前版本的 C++ 中)
最佳答案
不,该命令是不允许的;语言没有那么疯狂。
这些是完全不同的完整表达式,所以它由 [intro.execution]/16 控制:
Every value computation and side effect associated with a full-expression is sequenced before every value computation and side effect associated with the next full-expression to be evaluated.
关于c++ - 构造函数中初始化列表的评估顺序是否固定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44988879/