模板笔记

  1. 非类型模板形参
模板形参不必都是类型。
Template<class T,size_t N> void array_init(T (&parm) [N] )
{
    For ( size_t i=0;i!=N;i++){
        Parm[i]=0;
    }
}
例2:
template <int hi,int wid>
class Screen{
public:
    Screen():screen(hi*wid,'#'),cursor(0),height(hi),width(wid){}
    void dump(){std::cout<<screen;}
private:
    std::string screen;
    std::string::size_type cursor;
    std::string::size_type height,width;

};
int main( int argc, char **argv )
{
    Screen<24,80> hp2621;
    hp2621.dump();
    return 0;
}

 2、模板实参推断与函数指针

Template <typename T> int compare(const T& , const T&);

Int (*pf1) (const int&,const int&) = compare;

 

3、函数模板的显式实参

在某些情况下,不可能推断模板实参的类型。当函数的返回类型必须与形参表中所用的所有类型都不同时,最常出现这一问题。在这种情况下,有必要覆盖模板实参推断机制,并显式指定为模板形参所用的类型或值。
Template <calss T1,class T2,class T3>
T1 sum(T2,T3);

Long val3=sum<long>(I,lng) 

 

4、binary_function 

 

binary_function是STL定义的一个模板类,binary_function <A,B,C>定义了一个二元运算符函数执行的数据类型A 和 B 作为参数并返回数据类型 C。 对象作为基类使用类。
【所需头文件】
<functional>
【原型】
   template<class _A1, class _A2, class _R>
   struct binary_function
   {
     typedef _A1 first_argument_type;
     typedef _A2 second_argument_type;
     typedef _R result_type;
   };
示例代码:
 //////////////////////////////////////////// 
// 
// Compile options needed: /GX
// 
// binfunc.cpp : Illustrating the binary_function
//               structure.
// 
// Structure used: binary_function<A,B,C> - base
//                 class used to create operator
//                 functions taking data types A
//                 and B and returning data type C.
// 
// Written by Mark Hagen
// of Microsoft Product Support Services,
// Copyright (c) 1996 Microsoft Corporation.
// All rights reserved.
//////////////////////////////////////////// 

#include <functional>
#include <iostream>
using namespace std;

class binary_test : public binary_function<binary_test &,int,float>
{
public:
  float value;
  binary_test(){value=10.0;}
  binary_test(float x){value=x;}
  result_type operator<<(second_argument_type arg2);
};

binary_test::result_type
binary_test::operator<<(binary_test::second_argument_type arg2)
{
  value = ((int)value) << arg2;
  cout << "New value after shift is" << value << endl;
  return value;
}

void main(void)
{
  binary_test item;

  cout << "Begin" << endl;
  item = item << 2;
}
                
程序输出为: 
Begin
New value after shift is 40.0

顺便给出unary_function的定义:
template<class _Arg,
    class _Result>
    struct unary_function
    {    // base class for unary functions
    typedef _Arg argument_type;
    typedef _Result result_type;
    };

 

5、函数适配器

 函数适配器是用来以现有函数对象创建新函数对象的构件。因此,函数适配器类似于容器适配器。有多种函数适配器,包括函数对象求反。函数适配器仅用于这样的函数对象,它们以unary_fuction和binary_function这两种内建STL函数对象类型之一作为子类型。为什么有这种要求呢?因为函数适配器构造在重构函数对象时,需要知道它返回值的类型、参数的类型。如下,为自己实现的对一元操作的函数对象取反的适配器:

template<class _Type>
struct _not_stru{
    _not_stru(_Type& _tmp):_my_obj(_tmp){}
    typename _Type::result_type operator()(typename _Type::argument_type _val)
    {
        return !_my_obj(_val);
    }
    _Type _my_obj;
};
template<class _ct>
_not_stru<_ct> my_not(_ct _val){
    return _not_stru<_ct>(_val);
    
};
int main()
{
cout<<my_not(even())(3);
return 0;
}

 

6、typename

如下,编译器并不知道_Type::result_type是一种类型,所以必须在前面加上typename关键字。
template<class _Type>
struct _not_stru{
_not_stru(_Type& _tmp):_my_obj(_tmp){}
typename _Type::result_type operator()(typename _Type::argument_type _val)
{
return !_my_obj(_val);
}
_Type _my_obj;
};


posted on 2012-08-07 13:58  山本二十八  阅读(400)  评论(0编辑  收藏  举报

导航