mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-04-27 12:21:02 +08:00
support bazel complie this project and format code.
This commit is contained in:
30
practical_exercises/10_day_practice/day8/func/max_spec.cpp
Normal file
30
practical_exercises/10_day_practice/day8/func/max_spec.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
/* 模板特化.cpp */
|
||||
// Eg9-6.cpp
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
template <class T> T Max(T a, T b) { return (a > b) ? a : b; }
|
||||
//特化
|
||||
// template <> 返回类型 函数名<特化的数据类型>(参数表) {}
|
||||
template <> const char *Max<const char *>(const char *a, const char *b) {
|
||||
return (strcmp(a, b) >= 0) ? a : b;
|
||||
}
|
||||
template <> char *Max<char *>(char *a, char *b) {
|
||||
return (strcmp(a, b) >= 0) ? a : b;
|
||||
}
|
||||
int main() {
|
||||
float c = 5.1, d = 3.2;
|
||||
char s1[] = "xyce", s2[] = "xbv";
|
||||
cout << "2,3的最大值是:" << Max(3, 2) << endl;
|
||||
cout << "c,d的最大值是:" << Max(c, d) << endl;
|
||||
cout << Max("xbv", "xyce") << endl;
|
||||
cout << Max(s1, s2) << endl;
|
||||
|
||||
}
|
||||
/*
|
||||
① 当程序中同时存在模板和它的特化时,特化将被优先调用;
|
||||
②
|
||||
在同一个程序中,除了函数模板和它的特化外,还可以有同名的普通函数。其区别在于C++会对普通函数的调用实参进行隐式的类型转换,
|
||||
但不会对模板函数及特化函数的参数进行任何形式的类型转换。
|
||||
|
||||
*/
|
||||
Reference in New Issue
Block a user