如何实现泛型最大函数
How to implement generic max function?
我知道这是因为模板函数的返回类型与第一个参数(T(的返回类型相同。
如何修改此模板,使其在所有情况下都能正常运行?
#include <iostream>
using namespace std;
template <typename T, typename U>
T max(T x, U y)
{
return x>y ? x : y;
}
int main()
{
cout<<max(17.9,17)<<"n";
cout<<max(17,17.9)<<"n";
}
输出:
17.9
17
实现的行为是正确的,尽管您可能不需要该输出。代码中的返回类型存在问题。
如果可以使用 C++11,则可能需要使用尾随返回类型:
template <typename T, typename U>
auto max(T x, U y) -> decltype(x>y ? x : y) //C++11 only
{
return x>y ? x : y;
}
这将给出以下输出:
17.9
17.9
我希望这是期望的产出。
在线演示 : http://ideone.com/2Sh5Y
C++11 很容易,使用 std::common_type<> 特征:
template <typename T, typename U>
typename std::common_type<T,U>::type max(T x, U y) /// why not const& T and const& U ?
{
return x>y ? x : y;
}
common_type<>使用 decltype 关键字和 declval<> 特征,它们自 C++11 以来都是新的
输出正确。您从未指定过类型,因此抱怨它没有使用您希望它使用的类型是不合理的。如果需要特定类型,则必须确保两个参数都是该类型。
您可以将第一个参数强制转换为double
。或者您可以专门调用max<double, double>
。如果你真的,真的愿意,你也可以专门研究max<int, double>
和类似的组合。
C++03的答案。对于 C++11 - 使用 auto/decltype(请参阅其他答案(。
您必须创建另一个模板: template CommonNumericType<T1,T2>
:
template <typename L, typename R>
typename CommonNumericType<T1,T2>::Type max(L x, R y)
{
return x>y ? x : y;
}
并将此 CommonNumericType 专门用于每个可能的数值类型对:
template <typename L, typename R>
struct CommonNumericType;
template <typename T>
struct CommonNumericType<T,T> {
typedef T Type;
};
template <typename L>
struct CommonNumericType<L,long double> {
typedef long double Type;
};
template <typename R>
struct CommonNumericType<long double,R> {
typedef long double Type;
};
// ...
template <>
struct CommonNumericType<int,short> {
typedef int Type;
};
// and many others stuff
我可以考虑使一些数字类型层次结构 - 浮点类型在 int 类型之前 - 等等。因为<number of numeric types>^2
是相当大的数字:
template <typename T>
struct NumericTypeOrder;
template <>
struct NumericTypeOrder<long double> { enum { VALUE = 1 }; };
template <>
struct NumericTypeOrder<double> { enum { VALUE = 2 }; };
template <>
struct NumericTypeOrder<float> { enum { VALUE = 3 }; };
template <>
struct NumericTypeOrder<unsigned long long> { enum { VALUE = 4 }; };
// etc for all numeric types - where signed char is last one...
template <typename L, typename R, bool L_bigger_than_R>
struct CommonNumericTypeImpl;
template <typename L, typename R>
struct CommonNumericTypeImpl<L,R,true> {
typedef L type;
};
template <typename L, typename R>
struct CommonNumericTypeImpl<L,R,false> {
typedef R type;
};
template <typename L, typename R>
struct CommonNumericType
: CommonNumericTypeImpl<L,R,NumericTypeOrder<L>::value >= NumericTypeOrder<R>::value > {
};
或者只使用宏:
#define max(l,r) ((l) >= (r) ? (l) : (r))
简单得多,不是吗?
在 C++11 中,您可以使用类型推断(使用 auto
、 late 返回类型和 decltype
(:
template <typename T, typename U>
auto max(T x, U y) -> decltype(x>y ? x : y)
{
return x>y ? x : y;
}
- 链表的泛型函数remove()与成员函数remove)
- 以特征类型作为参数的泛型函数回调
- 传递非泛型函数的最有效方法是什么?
- 泛型函数反向打印任何矢量,编译器出错
- 是否可以在C++中创建泛型函数指针?
- 将泛型函数及其参数传递给元函数
- C++ 中的泛型函数持有者
- C++ 验证使泛型函数
- 两个不同流的泛型函数
- 如何覆盖 'const T&' 和 'T&&' 的泛型函数,其中 T 可以作为引用?
- 如何创建公开泛型函数的 DLL
- 为什么 std::apply 使用泛型函数失败
- 在某个值范围内生成 BST 的泛型函数
- 无法在循环中实例化泛型函数
- MISRA C++规则 14-5-1:在与类型关联的命名空间中声明的泛型函数模板的名称
- 使用泛型函数调用打印结构的所有嵌套成员变量
- C++ 使用数组作为参数创建泛型函数
- 泛型函数是否可以用于对结构求和
- C++为所有 std::sets 编写泛型函数
- 指向类成员函数的泛型函数指针