1 C++中的函数模板简介
函数模板:
- 一种特殊的函数可用不同类型进行调用。
- 看起来和普通函数很相似,区别是类型可被参数化。
- 函数模板是泛型编程在C++中的应用方式之一,是C++中重要的代码复用方式。

函数模板语法规则: - template关键字用于声明开始进行泛型编程。
- typename关键字用于声明泛指类型。

函数模板的使用:
- 自动类型推导调用。
- 具体类型显示调用。

函数模板使用示例:
#include <iostream>
#include <string>
using namespace std;
template < typename T >
void Swap(T& a, T& b)
{
T c = a;
a = b;
b = c;
}
template < typename T >
void Sort(T a[], int len)
{
for(int i=0; i<len; i++)
{
for(int j=i; j<len; j++)
{
if( a[i] > a[j] )
{
Swap(a[i], a[j]);
}
}
}
}
template < typename T >
void Println(T a[], int len)
{
for(int i=0; i<len; i++)
{
cout << a[i] << ", ";
}
cout << endl;
}
int main()
{
int a[5] = {5, 3, 2, 4, 1};
Println(a, 5);
Sort(a, 5);
Println(a, 5);
string s[5] = {"Java", "C++", "Pascal", "Ruby", "Basic"};
Println(s, 5);
Sort(s, 5);
Println(s, 5);
return 0;
}
2 函数模板深入理解
编译器从函数模板通过具体类型产生不同的函数,编译器会对函数模板进行两次编译:
- 对模板代码本身进行编译。
- 对参数替换后的代码进行编译。
注意事项:函数模板本身不允许隐式类型转换。
- 自动推导类型时,必须严格匹配。
- 显示类型指定时,能够进行隐式类型转换。
函数模板的本质示例程序:
#include <iostream>
#include <string>
using namespace std;
class Test
{
Test(const Test&);
public:
Test()
{
}
};
template < typename T >
void Swap(T& a, T& b)
{
T c = a;
a = b;
b = c;
}
typedef void(FuncI)(int&, int&);
typedef void(FuncD)(double&, double&);
typedef void(FuncT)(Test&, Test&);
int main()
{
FuncI* pi = Swap; // 编译器自动推导 T 为 int
FuncD* pd = Swap; // 编译器自动推导 T 为 double
// FuncT* pt = Swap; // 编译器自动推导 T 为 Test
cout << "pi = " << reinterpret_cast<void*>(pi) << endl;
cout << "pd = " << reinterpret_cast<void*>(pd) << endl;
// cout << "pt = " << reinterpret_cast<void*>(pt) << endl;
return 0;
}
3 多参数函数模板
函数模板可以定义任意多个不同的类型参数:

对于多参数函数模板:
- 无法自动推导返回值类型。
- 可以从左向右部分指定类型参数。

编程实验:多参数函数模板
#include <iostream>
#include <string>
using namespace std;
template
< typename T1, typename T2, typename T3 >
T1 Add(T2 a, T3 b)
{
return static_cast<T1>(a + b);
}
int main()
{
// T1 = int, T2 = double, T3 = double
int r1 = Add<int>(0.5, 0.8);
// T1 = double, T2 = float, T3 = double
double r2 = Add<double, float>(0.5, 0.8);
// T1 = float, T2 = float, T3 = float
float r3 = Add<float, float, float>(0.5, 0.8);
cout << "r1 = " << r1 << endl; // r1 = 1
cout << "r2 = " << r2 << endl; // r2 = 1.3
cout << "r3 = " << r3 << endl; // r3 = 1.3
return 0;
}
4 函数重载遇到函数模板
函数模板可以像普通函数一样被重载:
- C++编译器有限考虑普通函数。
- 如果函数模板可以产生一个更好的匹配,那么选择模板。
- 可以通过空模板实参列表限定编译器只匹配模板。

实例分析:重载函数模板
#include <iostream>
#include <string>
using namespace std;
template < typename T >
T Max(T a, T b)
{
cout << "T Max(T a, T b)" << endl;
return a > b ? a : b;
}
int Max(int a, int b)
{
cout << "int Max(int a, int b)" << endl;
return a > b ? a : b;
}
template < typename T >
T Max(T a, T b, T c)
{
cout << "T Max(T a, T b, T c)" << endl;
return Max(Max(a, b), c);
}
int main()
{
int a = 1;
int b = 2;
cout << Max(a, b) << endl; // 普通函数 Max(int, int)
cout << Max<>(a, b) << endl; // 函数模板 Max<int>(int, int)
cout << Max(3.0, 4.0) << endl; // 函数模板 Max<double>(double, double)
cout << Max(5.0, 6.0, 7.0) << endl; // 函数模板 Max<double>(double, double, double)
cout << Max('a', 100) << endl; // 普通函数 Max(int, int)
return 0;
}
5 函数模板的特化
函数模板只支持类型参数完全特化:

工程中的建议:当需要重载函数模板时,有限考虑使用模板特化;当模板特化无法满足需求时,再使用函数重载!
编程实验:函数模板的特化
#include <iostream>
#include <string>
using namespace std;
template
< typename T1, typename T2 >
class Test
{
public:
void add(T1 a, T2 b)
{
cout << "void add(T1 a, T2 b)" << endl;
cout << a + b << endl;
}
};
/*
template
< >
class Test < void*, void* > // 当 T1 == void* 并且 T2 == void* 时
{
public:
void add(void* a, void* b)
{
cout << "void add(void* a, void* b)" << endl;
cout << "Error to add void* param..." << endl;
}
};
*/
class Test_Void
{
public:
void add(void* a, void* b)
{
cout << "void add(void* a, void* b)" << endl;
cout << "Error to add void* param..." << endl;
}
};
template
< typename T >
bool Equal(T a, T b)
{
cout << "bool Equal(T a, T b)" << endl;
return a == b;
}
template
< >
bool Equal<double>(double a, double b)
{
const double delta = 0.00000000000001;
double r = a - b;
cout << "bool Equal<double>(double a, double b)" << endl;
return (-delta < r) && (r < delta);
}
bool Equal(double a, double b)
{
const double delta = 0.00000000000001;
double r = a - b;
cout << "bool Equal(double a, double b)" << endl;
return (-delta < r) && (r < delta);
}
int main()
{
cout << Equal( 1, 1 ) << endl;
cout << Equal(0.001, 0.001) << endl; // 优先使用普通函数
cout << Equal<>( 0.001, 0.001 ) << endl;
return 0;
}
6 数值型模板参数
模板参数可以是数值型参数(非类型参数):

数值型模板参数的限制:
- 变量不能作为模板参数。
- 浮点数不能作为模板参数。
- 类对象不能作为模板参数。
本质:模板参数是在编译阶段被处理的单元,因此,在编译阶段必须准确无误的唯一确定。
利用数值型模板参数求解如下问题:最高效的求解1 + 2 + 3 + 4 + … + N的值:
#include <iostream>
#include <string>
using namespace std;
template
< typename T, int N >
void func()
{
T a[N] = {0};
for(int i=0; i<N; i++)
{
a[i] = i;
}
for(int i=0; i<N; i++)
{
cout << a[i] << endl;
}
}
template
< int N >
class Sum
{
public:
static const int VALUE = Sum<N-1>::VALUE + N;
};
template
< >
class Sum < 1 >
{
public:
static const int VALUE = 1;
};
int main()
{
cout << "1 + 2 + 3 + ... + 10 = " << Sum<10>::VALUE << endl;
cout << "1 + 2 + 3 + ... + 100 = " << Sum<100>::VALUE << endl;
return 0;
}
参考资料:
917

被折叠的 条评论
为什么被折叠?



