这两个声明之间哪个是最可行的运算符 == 函数?

Which is the best viable operator == function between these two declarations?

#include <iostream>
template<typename T>
struct A{};
struct Y{
    template<typename T>
    bool operator==(A<T>){
        std::cout<<"#1\n";
        return true;
    }
};
template<typename T>
bool operator==(T,Y){
    std::cout<<"#2\n";
    return true;
}
int main(){ 
    A<int> a;
    Y y;
    a==y;
}

对于上面的代码片段,GCC 打印 #2 而 Clang 打印 #1。结果是here。我认为 Clang 是对的,因为 #2 是表达式 a==y 的非重写非成员候选者。相反,#1 的综合候选函数也是一个可行的函数。也就是说,重载集将由两个候选者组成,它们看起来像:

#1'
bool operator==(A<int>,Y); [with T = int] // synthesized candidate for #1

#2'
bool operator==(A<int>,Y);[with T = A<int>]

根据temp.func.order#3

If exactly one of the function templates was considered by overload resolution via a rewritten candidate ([over.match.oper]) with a reversed order of parameters, then the order of the function parameters in its transformed template is reversed.

P/A 对如下:

transformed type for #1: (A<uniqueT1>, Y) as A
original type for #2: (T, Y)  as P

transformed type for #2: (UniqueT2, Y) as A
original type for #1: (Y, A<UniqueT1>) as P

对于上述候选者,模板函数的偏序足以确定哪个是最可行的。这是子弹 over.match.best#2.5

for some argument j, ICSj(F1) is a better conversion sequence than ICSj(F2), or, if not that,

2.5 F1 and F2 are function template specializations, and the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in [temp.func.order], or, if not that,
[2.6 - 2.7]
2.8 F2 is a rewritten candidate ([over.match.oper]) and F1 is not

这意味着没有必要进入项目符号 2.8。根据上面的 P/A 对,#1 至少和 #2 一样专业,但 #2 至少没有 #1 专业;因此,#1' 是偏序中最可行的候选者。所以,这里的 Clang 应该是正确的。

但是,请考虑以下变体片段

#include <iostream>
template<class T>
struct A{};

class Y{};

template<class T>
bool operator==(Y,A<T>){
    std::cout<<"#1\n";
    return true;
}

template<class T>
bool operator ==(T,Y){
    std::cout<<"#2\n";
    return true;
}

int main(){
   A<int> a;
   Y y{};
   a == y;
}

此时,Clang 和 GCC 都认为 #2 是最可行的候选者。结果是here。然而,在我看来,这个例子与第一个相似。只是,将会员候选人变更为非会员候选人。同样,重载集将由两个候选者组成,它们看起来像:

#1''
bool operator==(A<int>,Y); [with T = int]  // synthesized candidate for #1

#2''
bool operator==(A<int>,Y); [with T = A<int>]

在这个例子中,部分排序也足以确定哪个候选者是最好的。所以,#1''应该还是最好的。为什么 Clang 和 GCC 都认为 #2 在这个例子中是最好的?

简短回答:您在示例中使用了不同的 Clang 版本,Clang 11 有正确的实现,而 Clang 10 没有。

在我的回答中,我详细说明了为什么 Clang 11 和 MSVC 在这两种情况下都是正确的,而 GCC 是错误的。


来自[over.match.oper]#3的候选人包括四组:

For [...] a binary operator @ [...] four sets of candidate functions, designated member candidates, non-member candidates, built-in candidates, and rewritten candidates, are constructed as follows:

在你的情况下,重写的候选人由[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.

在您的表达式 x == y 中,感兴趣的候选者是:

  • 成员候选人:候选人x.operator==(y)
  • 非会员候选人operator==(x, y)
  • 的候选人
  • rewritten candidates: y == x 的非改写 candidates y.operator==(x) and operator==(y, x).

让我们看看您的两个示例并确定最佳人选。


第一个例子

对于表达式 a == y 候选者是:

non-rewritten candidates:
    #2 via operator==(a, y)
rewritten candidates:
    #1 via y.operator==(a)

为了确定更好的候选者,我们需要应用 [over.match.best.general]#2,确定 F1 是否比 F2 更好的相关规则是:

(2.5) F1 and F2 are function template specializations, and the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in [temp.func.order], or, if not that,

[...]

(2.8) F2 is a rewritten candidate ([over.match.oper]) and F1 is not

如果我们将 #1 设为 F1 并将 #2 设为 F2 我们得到 #1 是更好的匹配,因为 (2.5) applies and it's considered before (2.8). Clang 11+ and the latest MSVC correctly select #1 as the better candidate here (demo).


第二个例子

对于表达式 a == y 候选者是:

non-rewritten candidates:
    #2 via operator==(a, y)
rewritten candidates:
    #1 via operator==(y, a)

应用 [over.match.best.general]#2 并将 #1 设为 F1 并将 #2 设为 F2,与第一个示例类似,我们得到 #1 是一个更好的候选者,因为 (2.5) 适用并且在 (2.8) 之前被考虑。与第一个示例相同 Clang 11+ 和最新的 MSVC 正确 select #1 作为更好的候选者 (demo).

Why do Clang and GCC both think #2 is the best in this example?

不要。在您的演示 link 中,您使用了 Clang 10,而在第一个示例中,您使用了 Clang 11。在这两种情况下,Clang 10 与 GCC 的结果相同。


在这两种情况下,#1 是更好的候选者,Clang 11+ 和最新的 MSVC 正确 select。 GCC 在这两种情况下都失败了,selecting #2。我的猜测是 GCC 对 [over.match.best.general]#2 的实现是不正确的,因为它错误地认为 (2.8)(2.5) 和 select在这两种情况下都是不可重写的候选者。