[英]How can I properly overload the > operator as a template function in C++? Learning template overloading
I want to do a regular operator overload, but with templates. 我想使用模板进行常规的运算符重载。 So imagine:
想象一下:
template <typename Type>
bool operator> (Type &tX, Type &tY)
{
return (tX.data > tY.data) ? tX : tY;
}
But I want it to work with any class or primitive. 但是我希望它可以与任何类或原始类型一起使用。 What is wrong with the code?
代码有什么问题?
How can I properly overload the > operator as a template function in C++?
如何在C ++中适当地将>运算符重载为模板函数?
You cannot overload the >
operator for primitive types. 您不能重载基本类型的
>
运算符。 The language does not allows that. 该语言不允许这样做。
You cannot overload the >
operator for user defined types in any sensible manner either. 您也不能以任何明智的方式为用户定义的类型重载
>
运算符。 The generic implementation has no way of knowing how one instance of a user defined type can be compared with another instance of the same type. 通用实现无法知道如何将用户定义类型的一个实例与相同类型的另一个实例进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.