gpt4 book ai didi

c++ - initializer_list c++11 中的评估顺序

转载 作者:可可西里 更新时间:2023-11-01 16:36:49 26 4
gpt4 key购买 nike

在下面的代码中,是否要求在 f2 之前调用 f1(或反之亦然),还是未指定?

int f1();
int f2();

std::initializer_list<int> list { f1(), f2() };

最佳答案

这是 C++ 标准的一个有趣的角落,其中执行顺序定义明确。第 8.5.4 节 [dcl.init.list],第 4 段:

Within the initializer-list of a braced-init-list, the initializer-clauses, including any that result from pack expansions (14.5.3), are evaluated in the order in which they appear. That is, every value computation and side effect associated with a given initializer-clause is sequenced before every value computation and side effect associated with any initializer-clause that follows it in the comma-separated list of the initializer-list.

因此在初始化列表中,函数调用是从左到右求值的。

关于c++ - initializer_list c++11 中的评估顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31333729/

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