Step By Step(C++模板函数)
[来源] 达内 [编辑] 达内 [时间]2012-09-12
存在这样一种函数,它们在行为上是完全一致的,而不同只是函数的参数类型。对于这种类型的函数,我们可以应用C++模板和泛型来帮助我们更优雅和更具技巧性的的解决一些程序设计上的实际问题。
一、模板函数实例化:
存在这样一种函数,它们在行为上是完全一致的,而不同只是函数的参数类型。对于这种类型的函数,我们可以应用C++模板和泛型来帮助我们更优雅和更具技巧性的的解决一些程序设计上的实际问题。如:
1 template <typename T> 2 inline T const & max(T const& a, T const & b) { 3 return a < b ? b : a; 4 }
1 int main() { 2 printf("The max value is %d\n ", ::max(4 ,5 )); //int max(int, int) 3 printf(" The max value is %f\n" , ::max( 4.2,5.3)); // float max(float, float) 4 return 0; 5 }
#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提供重载小于操作符的方法。如:< div style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 5px 0px; max-width: 900px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
1 class MyClass { 2 public: 3 MyClass(int value) : _value(value) { 4 } 5 bool operator < (const MyClass& other) const { //注意这里的操作符重载方法必需是const函数。 6 printf(" operator < is call\n" ); 7 return _value < other._value; 8 } 9 int myValue() const { return _value; } 10 private : 11 int _value; 12 }; 13 14 int main() 15 { 16 MyClass m1(30 ), m2(40); 17 printf("The max value is %d\n ",::max(m1,m2).myValue()); 18 return 0 ; 19 }
3. 函数参数传递的是引用,而不是传值。这样便可以在函数声明时返回应用类型。否则,如果函数参数是传值的,如:T const& max(T const a, T const b),那么对于该种情况,由于在比较时将会使用函数内部的两个临时变量,即实数a和b在函数内的副本。这样在函数返回时,如果返回临时变量的引用后果可想而知。因此可改为:T const max(T const a, T const b)。
4. C++的编译器会为每一种类型都产生一个实例化函数,而不是仅仅构造出一个函数实例,然后再基于不同的参数类型调用该通用函数。如: < div style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 5px 0px; max-width: 900px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
1 template <typename T> 2 inline T const & max(T const& a, T const & b) { 3 static int i = 0 ; 4 printf(" The value of i is %d\n" ,i++); 5 return a < b ? b : a; 6 } 7 8 class MyClass { 9 public : 10 MyClass(int value) : _value(value) { 11 } 12 bool operator < (const MyClass& other) const { 13 return _value < other._value; 14 } 15 int myValue() const { return _value; } 16 private: 17 int _value; 18 }; 19 20 int main() 21 { 22 ::max(4 ,5); // for max<int>23 ::max(5 ,6); // for max<int>24 ::max(5.3 ,5.4); // for max<float>25 ::max(5.3 ,5.4); // for max<float>26 MyClass m1(30 ), m2(40); 27 ::max(m1,m2); //for max<MyClass> 28 getch(); 29 return 0 ; 30 } 31 // The value of i is 0 32 // The value of i is 1 33 //The value of i is 0 34 //The value of i is 1 35 // The value of i is 0