具有运算符重载功能的c ++ 17评估顺序 [英] c++17 evaluation order with operator overloading functions
问题描述
关于这个问题
具有此规范
http://www.open- std.org/jtc1/sc22/wg21/docs/papers/2016/p0145r3.pdf
这是规范中的文字
此外,我们建议以下附加规则: 包含重载运算符的表达式的求值是 由与相应内置组件关联的顺序确定 运算符,而不是函数调用规则.
Furthermore, we suggest the following additional rule: the order of evaluation of an expression involving an overloaded operator is determined by the order associated with the corresponding built-in operator, not the rules for function calls.
这是否意味着这两个表达式不再等效?
Does this mean that these two expressions are no longer equivalent?
a << b;
operator<<(a, b);
由于第二个函数看起来像函数调用,因此参数中没有确定的评估顺序?
As the second one looks like a function call, hence there is no guaranteed evaluation order in the parameters?
推荐答案
由于第二个函数看起来像函数调用,因此参数中没有确定的评估顺序?"
的确. [expr.call]/5 包含一个示例,专门介绍了两者之间的区别您的问题[强调我的]中涉及的案例:
Indeed. [expr.call]/5 contains an example specifically covering the difference between the two cases covered in your question [emphasis mine]:
postfix-expression在 表达式列表和任何默认参数.初始化 参数,包括每个关联的值计算和边 效果,相对于其他任何元素不确定地排序 参数.
The postfix-expression is sequenced before each expression in the expression-list and any default argument. The initialization of a parameter, including every associated value computation and side effect, is indeterminately sequenced with respect to that of any other parameter.
...
注意:如果使用运算符符号调用了运算符功能, 参数评估按内置指令的指定顺序进行 运营商;看 [over.match.oper] . [示例:
Note: If an operator function is invoked using operator notation, argument evaluation is sequenced as specified for the built-in operator; see [over.match.oper]. [ Example:
struct S {
S(int);
};
int operator<<(S, int);
int i, j;
int x = S(i=1) << (i=2);
int y = operator<<(S(j=1), j=2);
执行初始化后,i
的值为2
(请参见
[expr.shift] ),但未指定 > j
的值是否为
1
或2
.
After performing the initializations, the value of i
is 2
(see
[expr.shift]), but it is unspecified whether the value of j
is
1
or 2
.
-示例]
这篇关于具有运算符重载功能的c ++ 17评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!