C++ Primer Plus 第五版 复习题7 函数模板 函数模板具体化
- #include
- struct box
- {
- int a;
- int b;
- int c;
- int d;
- };
- template
//函数模板 - T MaxValue (T x,T y)
- {
- return x > y ? x:y;
- }
- template <> box MaxValue (box a,box b) //函数模板具体化
- {
- return a.a >b.a ? a : b;
- }
- using namespace std;
- void main()
- {
- cout <<MaxValue(555,666)<<endl;
- cout <<MaxValue(1.1,0.5)<<endl;
- box a={1,2,3,4};
- box b={2,3,4,5};
- cout <<MaxValue(a,b).a<<endl; //重载函数模板具体化
- }
感谢您的阅读,本文由 smallwhite's Blog 版权所有。如若转载,请注明出处:smallwhite's Blog(https://smallwhite.ml/pub/uncategorized/c-primer-plus-di-wu-ban-fu-xi-ti-7-han-shu-mo-ban-han-shu-mo-ban-ju-ti-hua.html)