c++ primer学习之--------模板

     模板定义以关键字template开始,后接模板形参表,模板形参表是用尖括号扩住的一个或多个模板形参的列表,形参之间以逗号分隔。

 

     模板形参表不能为空。

 

     模板形参可以是表示类型的类型形参,也可以是表示常量表达式的非类型形参。类型形参跟在关键字class或typename之后定义。在函数模板形参表中,class和typename的意义是一样的,都指出后面所接的名字表示一个类型。

 

     如果是类型形参,该形参表示未知类型,如果是非类型形参,它是一个未知值。

 

1.     inline函数模板

              说明符放在模板形参表之后、返回类型之前,不能放在关键字template之前。

              template <typename T> inline T min(const T&, const T&);   //ok

              inline template <typename T> T min(const T&, const T&);  //error

2.      类模板

 

template <class Type> class Queue {
    public:
        Queue();
        Type &front();             //return element from head of Queue
        const Type &front() const;
        void push(const Type &);   //add element from head of Queue
        void pop();                //remove element from head of Queue
        bool empty() const;        // true if no elements in the Queue
    private:
};

 

        使用类模板时,必须为模板形参显示指定实参:

        Queue<int> qi;    //queue that holds ints

        Queue< vector<double> > qc;   //queue that holds vectors of doubles

        Queue<string> qs; //queue that holds strings

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值