构造函数中初始化列表的评估顺序是否固定?

Is the order of evaluation for initialization lists in constructors fixed?

我的印象是以下内容容易泄露

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{}} {};
};

如果评估顺序如下

  1. new A{}
  2. new int{3}
  3. new C{}
  4. a{}
  5. b{}
  6. 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.

每个成员初始化被视为一个单独的语句,并且这些语句之间的顺序被严格定义。

因此在您的示例中,订单将定义为:

  1. 新 A{}
  2. 一个{}
  3. 新整数{3}
  4. b{}
  5. 新 C{}
  6. c{}