c++ template(4) 基本技巧

时间:2021-09-03 17:04:36

1.关键字typename

这里内部的的typename指示T::const_iterator是一个类型,而不是一个静态成员.

#include <iostream> // print elements of an STL container template <typename T>
void printcoll (T const& coll)
{
    typename T::const_iterator pos;  // iterator to iterate over coll typename T::const_iterator end(coll.end());  // end position for (pos=coll.begin(); pos!=end; ++pos) {
        std::cout << *pos << ' ';
    }
    std::cout << std::endl;
}

c++ template(4) 基本技巧

2.使用this->

模板类继承后无法直接调用基类成员,需要使用this->修饰符来访问基类成员

template <typename T>
class Base
{
public:
    void exit();
};
template <typename T>
class Derive:public Base<T>
{
public:
    void foo()
    {
        this->exit();
        //or Base<T>::exit();
    }
};

3.模板赋值操作

默认情况下,相同类型的参数可以互相赋值,不同参数无法直接赋值.如下

        Stack<int>   intStack1,intStack2;       // stack of ints Stack<float> floatStack;     // stack of floats intStack1=intStack2;//ok floatStack=intStack1;//error 

重写=操作符

// assign stack of elements of type T2 template <typename T2>
Stack<T>& operator= (Stack<T2> const&);

实现

template <typename T>
 template <typename T2>
Stack<T>& Stack<T>::operator= (Stack<T2> const& op2)
{
    if ((void*)this == (void*)&op2) {    // assignment to itself? return *this;
    }

    Stack<T2> tmp(op2);              // create a copy of the assigned stack elems.clear();                   // remove existing elements while (!tmp.empty()) {           // copy all elements elems.push_front(tmp.top());
        tmp.pop();
    }
    return *this;
}

当然以上应用也可以使用局部特化

template <typename T, typename CONT = std::deque<T> >
class Stack {
  private:
    CONT elems;            // elements public:
    void push(T const&);   // push element void pop();            // pop element T top() const;         // return top element bool empty() const {   // return whether the stack is empty return elems.empty();
    }

    // assign stack of elements of type T2 template <typename T2, typename CONT2>
    Stack<T,CONT>& operator= (Stack<T2,CONT2> const&);
};

4.双重模板参数

模板参数也可以是一个模板类型

template <typename T,
          template <typename ELEM> class CONT = std::deque >
class Stack {
  private:
    CONT<T> elems;         // elements public:
    void push(T const&);   // push element void pop();            // pop element T top() const;         // return top element bool empty() const {   // return whether the stack is empty return elems.empty();
    }
};

5.模板参数初始化

template <typename T,
          template <typename ELEM> class CONT = std::deque >
class Stack {
  private:
    CONT<T> elems;         // elements public:
    void push(T const&);   // push element void pop();            // pop element T top() const;         // return top element bool empty() const {   // return whether the stack is empty return elems.empty();
    }
};

看起来似乎有点晕

6.模板参数值初始化

(1)函数

template <typename T>
void foo()
{
    //only declare T x;
    //initialize T x=T();
}

(2)类模板函数

template <typename T>
class MyClass
{
private:
    T x;
public:
    MyClass():x() {}
};
本文转自 http://www.cnblogs.com/Clingingboy/archive/2011/03/08/1977372.html