您的位置:首页 > 编程语言 > C语言/C++

Step By Step(C++模板函数)

2012-08-20 07:52 423 查看
[b]一、模板函数实例化:[/b]

存在这样一种函数,它们在行为上是完全一致的,而不同只是函数的参数类型。对于这种类型的函数,我们可以应用C++模板和泛型来帮助我们更优雅和更具技巧性的的解决一些程序设计上的实际问题。如:

template <typename T>
inline T const& max(T const& a, T const& b) {
return a < b ? b : a;
}


对于上面的模板函数,我们在实际的应用中可以用任意类型来实例化该模板函数,如:

int main() {
printf("The max value is %d\n", ::max(4,5));      //int max(int, int)
printf("The max value is %f\n", ::max(4.2,5.3));  //float max(float, float)
return 0;
}


在上面的实例中,C++编译器会通过调用参数将模板函数实例化,同时返回比较后的结果。当然,对于这样的方法我们也可以通过宏的方式来实现,如:
#define max(a,b) ((a) < (b)) ? (b) : (a)
相比于模板函数,宏的方式还是存在一些问题的,比如类型不安全。由于这个系列主要介绍的是C++模板,因此对于其他技术就不做更多的赘述了。对于模板函数max,有一些几点需要注意:
1. 参数a和参数b的类型要保持一致,否则编译器无法进行正确的类型推演。如:int a = max(4,3.3)。对于这种调用情况,我们可以修改为int a = max(4, (int)3.3),或者修改为int a = max<int>(4,3.3),即显示的指定模板函数max的类型参数。
2. 对于类型T的实际类型而言,该类型必需提供operator <() 的支持,否则无法成功编译。对于上面的示例,整型和浮点型均提供了小于操作符的内嵌支持,而对于其它的class而言,则需要给class提供重载小于操作符的方法。如:

class MyClass {
public:
MyClass(int value) : _value(value) {
}
bool operator< (const MyClass& other) const { //注意这里的操作符重载方法必需是const函数。
printf("operator < is call\n");
return _value < other._value;
}
int myValue() const { return _value; }
private:
int _value;
};

int main()
{
MyClass m1(30), m2(40);
printf("The max value is %d\n",::max(m1,m2).myValue());
return 0;
}


如果MyClass类没有提供bool operator< (const MyClass& other) const函数,该段代码将无法成功编译。
3. 函数参数传递的是引用,而不是传值。这样便可以在函数声明时返回应用类型。否则,如果函数参数是传值的,如:T const& max(T const a, T const b),那么对于该种情况,由于在比较时将会使用函数内部的两个临时变量,即实数a和b在函数内的副本。这样在函数返回时,如果返回临时变量的引用后果可想而知。因此可改为:T const max(T const a, T const b)。
4. C++的编译器会为每一种类型都产生一个实例化函数,而不是仅仅构造出一个函数实例,然后再基于不同的参数类型调用该通用函数。如:

template <typename T>
inline T const& max(T const& a, T const& b) {
static int i = 0;
printf("The value of i is %d\n",i++);
return a < b ? b : a;
}

class MyClass {
public:
MyClass(int value) : _value(value) {
}
bool operator< (const MyClass& other) const {
return _value < other._value;
}
int myValue() const { return _value; }
private:
int _value;
};

int main()
{
::max(4,5);     //for max<int>
::max(5,6);     //for max<int>
::max(5.3,5.4); //for max<float>
::max(5.3,5.4); //for max<float>
MyClass m1(30), m2(40);
::max(m1,m2);   //for max<MyClass>
getch();
return 0;
}
//The value of i is 0
//The value of i is 1
//The value of i is 0
//The value of i is 1
//The value of i is 0


模板函数max中的变量i是静态变量,该变量只会在函数第一次被调用时初始化一次。从输出结果可以看出,该变量被初始化三次,每一次都是基于不同类型的模板函数实例化。由此可见,max模板函数确实在代码段中被生成三份。

[b]二、重载函数模板:[/b]

和普通的函数重载一样,模板函数也可以实现函数重载的功能。如:

#include <stdio.h>
#include <conio.h>

inline int const& max(int const& a, int const& b) {
printf("int const& max(int const& a, int const& b) is called.\n");
return a < b ? b : a;
}

template <typename T>
inline T const& max(T const& a, T const& b) {
printf("T const& max(T const& a, T const& b) is called.\n");
return a < b ? b : a;
}

template <typename T>
inline T const& max(T const& a, T const& b, T const& c) {
printf("T const& max(T const& a, T const& b, T const& c) is called.\n");
return ::max(::max(a,b),c);
}

class MyClass {
public:
MyClass(int value) : _value(value) {
}
bool operator< (const MyClass& other) const {
printf("operator < is call\n");
return _value < other._value;
}
int myValue() const { return _value; }
private:
int _value;
};

int main() {
::max(5,43,68);    //三个参数的max<int>
printf("------------------------------------\n");
::max('a','b');    //两个参数的max<char>
::max(7,54);       //int参数类型的非模板函数。
::max<>(7,54);     //两个参数的max<int>
::max('a',43.2);   //int参数类型的非模板函数。
getch();
return 0;
}
//    T const& max(T const& a, T const& b, T const& c) is called.
//    int const& max(int const& a, int const& b) is called.
//    int const& max(int const& a, int const& b) is called.
//    ------------------------------------
//    T const& max(T const& a, T const& b) is called.
//    int const& max(int const& a, int const& b) is called.
//    T const& max(T const& a, T const& b) is called.
//    int const& max(int const& a, int const& b) is called.


从上面的输出可以看出,可以了解到以下几点:
1. 非模板函数和模板函数可以同名,在调用时可以一起进行函数重载的推演。
2. 编译器在进行函数重载的推演时,首选非模板类型的函数。
3. 通过max<>方式通知编译器,在推演时只考虑模板函数。
4. 由于模板函数在实例化的过程中,不会自动完成任何形式的类型转换,因此对于最后一个调用函数,由于两个参数的类型不一致,而普通函数max通过类型自动转换((int)'a',(int)43.2)可以覆盖该调用,所以编译器选择了该普通函数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: