gpt4 book ai didi

c++ - 用作成员构造函数参数的函数的求值顺序

转载 作者:行者123 更新时间:2023-11-28 01:31:34 26 4
gpt4 key购买 nike

假设一个类:

struct S {
Obj1 o1;
Obj2 o2;

S() : o2( fn2() ), o1( fn1(), fn3() ) {}
};

在这种情况下,保证先排序的是什么? o1() 在 o2() 之前排序。 fn1() 和 fn3() 在 o1() 之前排序,fn2() 在 o2() 之前排序。问题是 fns 的前序。据我了解, fn1() 和 fn3() 可能以未指定的顺序完成。 fn1() 和 fn3() 是否保证在 fn2() 之前排序?

最佳答案

[intro.execution]/12 A full-expression is...

(12.3) — an init-declarator (Clause 11) or a mem-initializer (15.6.2), including the constituent expressions of the initializer...

[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.

因此,o1( fn1(), fn3() ) 及其所有子表达式和副作用的调用在 o2( fn2( ) )

如果您仍然不相信,[class.base.init]/7 中有这个非规范性注释:

[ Note: The initialization performed by each mem-initializer constitutes a full-expression (4.6). Any expression in a mem-initializer is evaluated as part of the full-expression that performs the initialization. —end note ]

关于c++ - 用作成员构造函数参数的函数的求值顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51330575/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com