gpt4 book ai didi

c++ - 对函数参数的要求是否也适用于初始化列表?

转载 作者:可可西里 更新时间:2023-11-01 17:39:57 25 4
gpt4 key购买 nike

所以我在这里阅读:https://stackoverflow.com/a/598150/2642059这是非法的:

foo(i++, i++);

但我相信那是因为没有强制序列,据我所知,初始化列表就是这种情况。那么这是法典吗?

const int foo[] = { i++, i++ };

最佳答案

是的,初始化子句的求值顺序在花括号初始化列表中得到保证。

来自标准,§11.6.4/4 List-initialization [dcl.init.list] :

(强调我的)

Within the initializer-list of a braced-init-list, the initializer-clauses, including any that result from pack expansions, 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. [ Note: This evaluation ordering holds regardless of the semantics of the initialization; for example, it applies when the elements of the initializer-list are interpreted as arguments of a constructor call, even though ordinarily there are no sequencing constraints on the arguments of a call. — end note ]

来自 cppreference.com :

Every initializer clause is sequenced before any initializer clause that follows it in the braced-init-list. This is in contrast with the arguments of a function call expression, which are unsequenced.

标准注释示例,

struct A { A(int, int) {} };
...
int i = 0;
A a1(i++, i++); // used as the arguments of the constructor; unsequenced
A a2{i++, i++}; // used as the arguments of the constructor; sequenced, within the initializer-list of a braced-init-list

关于c++ - 对函数参数的要求是否也适用于初始化列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48266525/

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