构造函数的参数求值顺序 [英] order of evaluation of arguments to a constructor

查看:61
本文介绍了构造函数的参数求值顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有这个课程:

struct A
{
  A(int, int, int) {}
};

,我将其初始化为:

A{ a(), b(), c() };

其中函数 a() b() c()都返回 int 。应该在 b() b() a() c()之前的$ c>?

Where the functions a(), b() and c() all return int. Should a() be called before b() and b() before c()?

我对标准中的以下段落感到困惑(8.5.4 [dcl.init.list] p4):

I am mystified by the following paragraph from the standard (8.5.4 [dcl.init.list] p4):


在支撑列表的初始列表中,初始子句,包括由pack
扩展产生的结果(14.5.3),将按照它们出现的顺序进行评估。也就是说,与给定的初始化程序子句相关联的每个值计算和
副作用在与它之后的任何跟在其后的初始化程序子句相关联的每个值计算和
副作用之前被排序。初始化列表。
[注意:无论初始化的语义如何,此求值顺序均成立;例如,当$ initial-list的元素被解释为构造函数调用的参数时,它将应用
,即使
通常在调用的参数上没有排序约束。 —尾注]

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

根据引号,将按出现的顺序调用这些函数,但是当我使用我的编译器(g ++-4.8.1),没有保存。我误解了吗?

According to the quote, the functions would be called in order they appear, but when I've tested this with my compiler (g++-4.8.1), it did not hold. Have I misunderstood something?

推荐答案

这是 GCC错误(由于 Casey (链接)。您引用的段落通常适用于列表初始化,其中条款在第8.5.4 / 1段中非常清楚地定义:

This is a bug in GCC (thanks to Casey for the link). The paragraph you quoted applies in general to list initialization, where the terms are defined pretty clearly in paragraph 8.5.4/1:


List-initialization 是对 braised-init-list 中的对象或引用的初始化。这样的初始化程序是称为 initializer list
,列表中以逗号分隔的 initializer-clauses 称为的 elements
初始化程序列表。

List-initialization is initialization of an object or reference from a braced-init-list. Such an initializer is called an initializer list, and the comma-separated initializer-clauses of the list are called the elements of the initializer list.

没有理由相信这仅适用于初始化程序列表构造函数的调用。另外,您引用的段落中的注释澄清了:

There is no reason to believe this should apply only to the invocation of an initializer list constructor. Also, the note in the paragraph you quoted clarifies that:


此评估顺序成立,而与初始化;例如,即使通常对调用的参数没有排序约束,它也会应用初始化程序列表的元素解释为构造函数调用的参数

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.

这篇关于构造函数的参数求值顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆