【问题标题】:Do the Requirements Placed on Function Arguments Also Apply to Initializer Lists?对函数参数的要求是否也适用于初始化列表?
【发布时间】:2018-06-24 07:01:25
【问题描述】:

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

foo(i++, i++);

但我相信这是因为没有强制序列,我理解初始化列表就是这种情况。那么这是法律法规吗?

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

【问题讨论】:

    标签: c++ arguments language-lawyer initializer-list sequencing


    【解决方案1】:

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

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

    (强调我的)

    在花括号初始化列表的初始化列表中, 初始化子句,包括任何由包扩展产生的子句, 按照它们出现的顺序进行评估。也就是说,每个值 与给定初始化子句相关的计算和副作用 每个相关的值计算和副作用之前排序 以逗号分隔的任何初始化子句 初始化列表的列表。 [ 注:此评估排序成立 无论初始化的语义如何;例如,它 当初始化列表的元素被解释为 构造函数调用的参数,即使通常没有 对调用参数的排序约束。 — 尾注 ]

    来自cppreference.com

    每个初始化子句都是sequenced before any 在花括号初始化列表中跟随它的初始化子句。这是在 与function call expression 的参数对比, 哪个是 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
    

    【讨论】:

    • 您能否提供note 部分的示例,即构造函数调用部分。
    • @GauravSehgal 添加到答案中。
    猜你喜欢
    • 1970-01-01
    • 2017-05-15
    • 2017-11-19
    • 1970-01-01
    • 2023-04-10
    • 2016-12-13
    • 2016-07-19
    • 1970-01-01
    • 2021-12-06
    相关资源
    最近更新 更多