这是 != 运算符的重写候选者
which are the rewritten candidates for != operator
标准中的相关规则说:
over.match.oper#3.4.3
The rewritten candidate set is determined as follows:
- For the relational ([expr.rel]) operators, the rewritten candidates include all non-rewritten candidates for the expression x <=> y.
- For the relational ([expr.rel]) and three-way comparison ([expr.spaceship]) operators, the rewritten candidates also include a synthesized candidate, with the order of the two parameters reversed, for each non-rewritten candidate for the expression y <=> x.
- For the != operator ([expr.eq]), the rewritten candidates include all non-rewritten candidates for the expression x == y.
- For the equality operators, the rewritten candidates also include a synthesized candidate, with the order of the two parameters reversed, for each non-rewritten candidate for the expression y == x.
- For all other operators, the rewritten candidate set is empty.
考虑以下 code:
#include <iostream>
struct Data{
bool operator ==(int c){
return true;
}
};
int main(){
Data d;
bool r = 0 != d; // should be ill-formed
}
不知道为什么编译器能编译出代码。 IMO,表达式 0!=d
的重写候选应包括表达式 x == y 的 所有非重写候选 。我对措辞 non-rewritten candidates 的理解是,考虑除表达式 x==y 的重写候选之外的任何候选,用于操作 x != y。毕竟,子弹4是用来组成重写的候选集的。所以,第四点不应该继续适用于为原始表达式x!=y
重写的x==y
。在我的示例中,0!=d
的重写候选集仅包含 0==d
的候选集,不应考虑 d==0
;
的任何重写候选集
这个page中的相关规定,关于不可重写的措辞,和我的解释是一样的。 (不包括改写的候选人)
For the != operator ([expr.eq]), the rewritten candidates include all member, non-member, and built-in candidates for the expression x == y.
不过,继续看下一段,即:
If a rewritten operator== candidate is selected by overload resolution for an operator @, its return type shall be cv bool, and x @ y is interpreted as:
- if @ is != and the selected candidate is a synthesized candidate with reversed order of parameters, !(y == x),
既然!=
改写的候选项不能继续改写,怎么会有参数顺序颠倒的合成候选项呢?我不知道为什么。 !=
重写候选项的规则如何解读,特别是非重写候选项的措辞和最后一条规则?
我怀疑这是 [over.match.oper]/3.4.4 授予的:
For the equality operators, the rewritten candidates also include a synthesized candidate, with the order of the two parameters reversed, for each non-rewritten candidate for the expression y == x.
相等运算符是operator==
和operator!=
。另请注意,“综合”与“重写”不是同义词:四组候选人分别是会员候选人、非会员候选人、内置候选和重写候选。综合候选仅属于这些集合之一(在本例中为重写候选集合)。
因此,我的理解是 x != y
的重写候选包括
x == y
的所有未重写候选,以及
y == x
. 的非重写候选的综合候选
所以 y == x
的合成候选也是重写的候选。然后 [over.match.oper]/9.1 开始:
if @
is !=
and the selected candidate is a synthesized candidate with reversed order of parameters, !(y == x)
即如果重写的候选是上面合成的y == x
候选,那么结果就是!(y == x)
.
标准中的相关规则说:
over.match.oper#3.4.3
The rewritten candidate set is determined as follows:
- For the relational ([expr.rel]) operators, the rewritten candidates include all non-rewritten candidates for the expression x <=> y.
- For the relational ([expr.rel]) and three-way comparison ([expr.spaceship]) operators, the rewritten candidates also include a synthesized candidate, with the order of the two parameters reversed, for each non-rewritten candidate for the expression y <=> x.
- For the != operator ([expr.eq]), the rewritten candidates include all non-rewritten candidates for the expression x == y.
- For the equality operators, the rewritten candidates also include a synthesized candidate, with the order of the two parameters reversed, for each non-rewritten candidate for the expression y == x.
- For all other operators, the rewritten candidate set is empty.
考虑以下 code:
#include <iostream>
struct Data{
bool operator ==(int c){
return true;
}
};
int main(){
Data d;
bool r = 0 != d; // should be ill-formed
}
不知道为什么编译器能编译出代码。 IMO,表达式 0!=d
的重写候选应包括表达式 x == y 的 所有非重写候选 。我对措辞 non-rewritten candidates 的理解是,考虑除表达式 x==y 的重写候选之外的任何候选,用于操作 x != y。毕竟,子弹4是用来组成重写的候选集的。所以,第四点不应该继续适用于为原始表达式x!=y
重写的x==y
。在我的示例中,0!=d
的重写候选集仅包含 0==d
的候选集,不应考虑 d==0
;
这个page中的相关规定,关于不可重写的措辞,和我的解释是一样的。 (不包括改写的候选人)
For the != operator ([expr.eq]), the rewritten candidates include all member, non-member, and built-in candidates for the expression x == y.
不过,继续看下一段,即:
If a rewritten operator== candidate is selected by overload resolution for an operator @, its return type shall be cv bool, and x @ y is interpreted as:
- if @ is != and the selected candidate is a synthesized candidate with reversed order of parameters, !(y == x),
既然!=
改写的候选项不能继续改写,怎么会有参数顺序颠倒的合成候选项呢?我不知道为什么。 !=
重写候选项的规则如何解读,特别是非重写候选项的措辞和最后一条规则?
我怀疑这是 [over.match.oper]/3.4.4 授予的:
For the equality operators, the rewritten candidates also include a synthesized candidate, with the order of the two parameters reversed, for each non-rewritten candidate for the expression y == x.
相等运算符是operator==
和operator!=
。另请注意,“综合”与“重写”不是同义词:四组候选人分别是会员候选人、非会员候选人、内置候选和重写候选。综合候选仅属于这些集合之一(在本例中为重写候选集合)。
因此,我的理解是 x != y
的重写候选包括
x == y
的所有未重写候选,以及y == x
. 的非重写候选的综合候选
所以 y == x
的合成候选也是重写的候选。然后 [over.match.oper]/9.1 开始:
if
@
is!=
and the selected candidate is a synthesized candidate with reversed order of parameters,!(y == x)
即如果重写的候选是上面合成的y == x
候选,那么结果就是!(y == x)
.