简体   繁体   English

重载逗号运算符*是否真的影响其操作数的评估顺序?

[英]Does overloading the comma operator *really* affect the order of evaluation of its operands?

The comma operator guarantees left-to-right evaluation order. 逗号运算符保证从左到右的评估顺序。

[n3290: 5.18/1] : The comma operator groups left-to-right. [n3290: 5.18/1] :逗号运算符从左到右分组。

 expression: assignment-expression expression , assignment-expression 

A pair of expressions separated by a comma is evaluated left-to-right ; 用逗号分隔的一对表达式从左到右进行评估 ; the left expression is a discarded value expression (Clause 5). 左表达式是废弃的值表达式(第5条)。 Every value computation and side effect associated with the left expression is sequenced before every value computation and side effect associated with the right expression. 在与右表达式相关联的每个值计算和副作用之前,对与左表达式相关联的每个值计算和副作用进行排序。 The type and value of the result are the type and value of the right operand; 结果的类型和值是右操作数的类型和值; the result is of the same value category as its right operand, and is a bit-field if its right operand is a glvalue and a bit-field. 结果与右操作数具有相同的值类别,如果右操作数是glvalue和位域,则是一个位域。

The only other clause regarding the operator doesn't mention evaluation order. 关于运营商的唯一其他条款没有提到评估顺序。 So it would appear that this is still the case when the operator has been overloaded. 因此,当操作员过载时,情况似乎仍然存在。

But then, further up, in the general spiel regarding expressions, of course it is stated that the rules change when you overload operators: 但是,接下来,在关于表达式的一般方面,当然,当你重载运算符时,规则会发生变化:

[n3290: 5/2]: [ Note: Operators can be overloaded, that is, given meaning when applied to expressions of class type (Clause 9) or enumeration type (7.2). [n3290: 5/2]: [ 注意:操作符可以重载,也就是说,当应用于类类型(第9节)或枚举类型(7.2)的表达式时给定含义。 Uses of overloaded operators are transformed into function calls as described in 13.5. 重载运算符的使用将转换为函数调用,如13.5中所述。 Overloaded operators obey the rules for syntax specified in Clause 5, but the requirements of operand type, value category, and evaluation order are replaced by the rules for function call. 重载运算符遵循第5章中指定的语法规则,但操作数类型,值类别和评估顺序的要求将替换为函数调用的规则。 Relations between operators, such as ++a meaning a+=1 , are not guaranteed for overloaded operators (13.5), and are not guaranteed for operands of type bool . 运算符之间的关系,例如++a表示a+=1 ,不能保证重载运算符(13.5),并且不保证bool类型的操作数。 —end note ] - 尾注 ]

However, this is non-normative text. 但是,这是非规范性文本。 Is there any normative text defining this rule, or could a compliant compiler ignore it? 是否有任何规范性文本定义此规则,或者兼容的编译器是否可以忽略它?

I only have the 03 standard to hand, but in it 5/3 says "Clause 5 defines the effects of operators when applied to types for which they have not been overloaded." 我只有03标准,但在其中5/3表示“第5条定义了运算符在应用于尚未超载的类型时的效果。”

So all of clause 5, including 5.18/1, only applies to the built-in operators and not to any overloaded function. 因此,第5节(包括5.18 / 1)的所有内容仅适用于内置运算符,而不适用于任何重载函数。

(A compliant compiler could always evaluate the operands to an overloaded operator ,() left to right though.) (兼容的编译器总是可以将操作数计算为重载operator ,()从左到右计算。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM