我想在模板类的内部类上支持全局二进制运算符:
template <unsigned N, typename T>
class Parent
{
public:
class Child
{
public:
Child & operator |= (const Child &);
};
};
template <unsigned N, typename T>
typename Parent<N,T>::Child
operator | (
const typename Parent<N,T>::Child & a,
const typename Parent<N,T>::Child & b)
{
return Parent<N,T>::Child (a) |= b;
}
int main ()
{
Parent<1,char>::Child a;
Parent<1,char>::Child b;
auto c = a | b;
}
在我看来,表达式a|b
与operator|
原型一致应该没有问题,但是(gcc 7.3.0):
error: no match for ‘operator|’ (operand types are ‘Parent<1, char>::Child’ and ‘Parent<1, char>::Child’)
...
template argument deduction/substitution failed:
couldn't deduce template parameter ‘N’
请注意,这可行:
auto c = Parent<1,char>::Child(a) |= b
为什么全局运算符不起作用?