模板1

半城伤御伤魂 提交于 2020-02-02 05:12:29

template<typename T>

inline T const& max(T const& a,T const& b)

max(4, 7);//ok

max(4 , 7.1);//error

解决办法:

1. max(static_cast<double>(4), 7.1);

2.max<double>(4, 7.1);

 

#include <iostream>

using namespace std;

template<typename T>
inline T const& max(T const& a, T const& b)
{
return a > b ? a : b;
}
int main()
{
cout << ::max(4, 5) << endl;
cout << ::max('a', 'b') << endl;;
cout << ::max(4.1, 5.1) << endl;;
cout << ::max(4, static_cast<int>(5.1)) << endl;;
cout << ::max(static_cast<double>(4), 5.1) << endl;;
cout << ::max<double>(4, 5.1) << endl;;
cout << ::max<int>(4, 5.1) << endl;;
return 0;
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!