我想用模板模板参数重载模板类中的相等运算符。代码:
template <typename TObj, template<typename T> class TDerived>
class A;
template <typename TObjL, template<typename T> class TDerivedL, typename TObjR, template<typename T> class TDerivedR>
bool operator==(const A<TObjL, TDerivedL>& l, const A<TObjR, TDerivedR>& r)
{
return true;
}
template <typename TObj, template<typename T> class TDerived>
class A
{
template <typename TObjL, template<typename T> class TDerivedL, typename TObjR, template<typename T> class TDerivedR>
friend bool operator==(const A<TObjL, TDerivedL>& l, const A<TObjR, TDerivedR>& r);
};
template <typename TObj>
class B: public A<TObj, B>
{
};
int main()
{
B<int> b1, b2;
b1==b2;
return 0;
}
编译器拒绝编译此代码,给我一个错误:'operator =='的模糊重载。当我用类型参数替换所有模板模板参数时(例如,仅留下class TDerived
),没有错误。为什么会出现这种错误?在这些类中重载运算符的正确方法是什么?