为什么可以转换的泛型类型不能隐式转换?
Why does a generic type that can be casted not get implicitly converted?
我有一个 class A
和一个 class B
,它们都是带有类型参数 T
的泛型。 A<T>
的对象可以转换为 B<T>
。我在 B
上有一个通用运算符重载,我希望能够调用 A
对象和 B
对象,其中 A
对象被隐式转换。
当我尝试这个时它没有编译:
template <typename T>
class A {};
template <typename T>
class B {
public:
B() {}
B(const A<T> &a) {}
};
template <typename T>
B<T> operator*(const B<T> &obj1, const B<T> &obj2) {
return B<T>(); // doesn't matter
}
int main() {
A<int> objA;
B<int> objB;
B<int> combined1 = objA * objB; // error: operator* isn't defined on these types
B<int> combined2 = static_cast<B<int>>(objA) * objB; // fine
return 0;
}
然而,当 A 和 B 不是通用的时,它工作正常:
class A {};
class B {
public:
B() {}
B(const A &a) {}
};
B operator*(const B &obj1, const B &obj2) {
return B(); // doesn't matter
}
int main() {
A objA;
B objB;
B combined1 = objA * objB; // fine
B combined2 = static_cast<B>(objA) * objB; // also fine
return 0;
}
这是为什么?使运算符重载泛型是否意味着无法推断类型?
通常,在进行参数推导时不允许隐式转换,我可以认为 派生到 base 是允许的。表达式
B<int> combined1 = objA * objB;
期望为 objA * objB
找到可行的重载,包括 ADL 找到的重载,一种可能是:
template <typename T>
B<T> operator*(const A<T> &obj1, const B<T> &obj2) {...}
但找到了 none,您提供的重载不是候选者,因此调用失败,但是如果您向运算符提供显式模板参数,那么将没有任何可推断的和隐式的通过转换构造函数的转换将允许调用:
B<int> combined1 = operator*<int>(objA, objB);
但我不会那样做,坚持使用它更好地解释意图。
你可以在class A
中定义友元函数来调用你的模板函数
template <class T>
class B;
template <typename T>
class A {
friend B<T> operator*(const B<T> &obj1, const B<T> &obj2) {} # here call template function
};
template <typename T>
class B {
public:
B() {}
B(const A<T> &a) {}
};
template <typename T>
B<T> operator*(const B<T> &obj1, const B<T> &obj2) {
return B<T>(); // doesn't matter
}
int main() {
A<int> objA;
B<int> objB;
B<int> combined1 = objA * objB; // fine
B<int> combined2 = static_cast<B<int>>(objA) * objB; // fine
return 0;
}
在参数推导过程中,conversion/promotion不会出现,所以对于
objA * objB
检查候选人超载的有效性时,T
不能推导出:
template <typename T> B<T> operator*(const B<T> &, const B<T> &);
因此拒绝重载。
解决这个问题的一种方法是创建一个非模板函数。因为它应该适用于类模板,一种方法是使用 friend
函数:
template <typename T>
class B {
public:
B() {}
B(const A<T>&) {}
friend B operator*(const B&, const B&) { return /*...*/; }
};
现在,objA * objB
考虑过载 B<int> operator*(const B<int>&, const B<int>&)
并且可以进行转换以查看函数是否可行(确实可行)。
我有一个 class A
和一个 class B
,它们都是带有类型参数 T
的泛型。 A<T>
的对象可以转换为 B<T>
。我在 B
上有一个通用运算符重载,我希望能够调用 A
对象和 B
对象,其中 A
对象被隐式转换。
当我尝试这个时它没有编译:
template <typename T>
class A {};
template <typename T>
class B {
public:
B() {}
B(const A<T> &a) {}
};
template <typename T>
B<T> operator*(const B<T> &obj1, const B<T> &obj2) {
return B<T>(); // doesn't matter
}
int main() {
A<int> objA;
B<int> objB;
B<int> combined1 = objA * objB; // error: operator* isn't defined on these types
B<int> combined2 = static_cast<B<int>>(objA) * objB; // fine
return 0;
}
然而,当 A 和 B 不是通用的时,它工作正常:
class A {};
class B {
public:
B() {}
B(const A &a) {}
};
B operator*(const B &obj1, const B &obj2) {
return B(); // doesn't matter
}
int main() {
A objA;
B objB;
B combined1 = objA * objB; // fine
B combined2 = static_cast<B>(objA) * objB; // also fine
return 0;
}
这是为什么?使运算符重载泛型是否意味着无法推断类型?
通常,在进行参数推导时不允许隐式转换,我可以认为 派生到 base 是允许的。表达式
B<int> combined1 = objA * objB;
期望为 objA * objB
找到可行的重载,包括 ADL 找到的重载,一种可能是:
template <typename T>
B<T> operator*(const A<T> &obj1, const B<T> &obj2) {...}
但找到了 none,您提供的重载不是候选者,因此调用失败,但是如果您向运算符提供显式模板参数,那么将没有任何可推断的和隐式的通过转换构造函数的转换将允许调用:
B<int> combined1 = operator*<int>(objA, objB);
但我不会那样做,坚持使用它更好地解释意图。
你可以在class A
中定义友元函数来调用你的模板函数
template <class T>
class B;
template <typename T>
class A {
friend B<T> operator*(const B<T> &obj1, const B<T> &obj2) {} # here call template function
};
template <typename T>
class B {
public:
B() {}
B(const A<T> &a) {}
};
template <typename T>
B<T> operator*(const B<T> &obj1, const B<T> &obj2) {
return B<T>(); // doesn't matter
}
int main() {
A<int> objA;
B<int> objB;
B<int> combined1 = objA * objB; // fine
B<int> combined2 = static_cast<B<int>>(objA) * objB; // fine
return 0;
}
在参数推导过程中,conversion/promotion不会出现,所以对于
objA * objB
检查候选人超载的有效性时,T
不能推导出:
template <typename T> B<T> operator*(const B<T> &, const B<T> &);
因此拒绝重载。
解决这个问题的一种方法是创建一个非模板函数。因为它应该适用于类模板,一种方法是使用 friend
函数:
template <typename T>
class B {
public:
B() {}
B(const A<T>&) {}
friend B operator*(const B&, const B&) { return /*...*/; }
};
现在,objA * objB
考虑过载 B<int> operator*(const B<int>&, const B<int>&)
并且可以进行转换以查看函数是否可行(确实可行)。