【发布时间】:2018-07-20 02:21:13
【问题描述】:
为什么这不起作用,因为有一个隐式“构造”选项?
class A {};
template<typename T>
class Type {
public:
Type() = default;
Type(T* ptr) {
}
};
template<typename T>
bool operator==(Type<T> l, Type<T> r) {
return true;
}
int main() {
A a;
Type<A> type(&a);
bool v = (type == &a); // Does not work
//bool v = (type == Type<A>(&a)); // That would work
}
为什么不使用带有(&base,即A*)的隐式构造Type<A>?
我怎样才能编写此代码以使其工作?
【问题讨论】:
-
您可能需要使用类似:
template<typename T, typename U> bool operator==(Type<T> l, Type<U> r).
标签: c++ oop templates operator-overloading