对函数参数的要求是否也适用于初始化列表?
Do the Requirements Placed on Function Arguments Also Apply to Initializer Lists?
所以我在这里读到: 这是非法的:
foo(i++, i++);
但我相信那是因为没有强制序列,据我所知,初始化列表就是这种情况。那么这是法典吗?
const int foo[] = { i++, i++ };
是的,初始化子句的求值顺序在 braced-init-list 中得到保证。
根据标准,§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 ]
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
所以我在这里读到: 这是非法的:
foo(i++, i++);
但我相信那是因为没有强制序列,据我所知,初始化列表就是这种情况。那么这是法典吗?
const int foo[] = { i++, i++ };
是的,初始化子句的求值顺序在 braced-init-list 中得到保证。
根据标准,§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 ]
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