1.可以将类型作为参数传进；
2.可以传进不同类型的参数；

## 模板类

template <class T> //声明一个模板，虚拟类型名为T。注意：这里没有分号。
class Compare //类模板名为Compare
{

public :
Compare(T a,T b)
{

x=a;y=b;
}
T max( )
{

return (x>y)?x:y;
}
T min( )
{

return (x<y)?x:y;
}
private :
T x,y;
};



template<typename _Tp, int chs> class Mat_ {

public:
typedef _Tp value_type;

// default constructor
Mat_() : rows(0), cols(0), channels(0), data(NULL), step(0), allocated(false), datastart(NULL), dataend(NULL) {

}
// constructs 2D matrix of the specified size
Mat_(int _rows, int _cols);
// constucts 2D matrix and fills it with the specified value _s
Mat_(int _rows, int _cols, const Scalar& _s);
// constructor for matrix headers pointing to user-allocated data, no data is copied
Mat_(int _rows, int _cols, void* _data);
// copy constructor, NOTE: deep copy
Mat_(const Mat_<_Tp, chs>& _m);
Mat_& operator = (const Mat_& _m);

// reports whether the matrix is continuous or not
bool isContinuous() const;
// returns true if the matrix is a submatrix of another matrix
bool isSubmatrix() const;

// copies the matrix content to "_m"
void copyTo(Mat_<_Tp, chs>& _m, const Rect& rect = Rect(0, 0, 0, 0)) const;

// return typed pointer to the specified matrix row,i0, A 0-based row index
const uchar* ptr(int i0 = 0) const;
uchar* ptr(int i0 = 0);

// no data is copied, no memory is allocated
void getROI(Mat_<_Tp, chs>& _m, const Rect& rect = Rect(0, 0, 0, 0));
// Locates the matrix header within a parent matrix
void locateROI(Size& wholeSize, Point& ofs) const;
// Adjusts a submatrix size and position within the parent matrix
void adjustROI(int dtop, int dbottom, int dleft, int dright);

// value converted to the actual array type
void setTo(const Scalar& _value);

// Converts an array to another data type with optional scaling
// the method converts source pixel values to the target data type
// if it does not have a proper size before the operation, it is reallocated
// \f[m(x,y) = saturate \_ cast<rType>( \alpha (*this)(x,y) +  \beta )\f]
template<typename _Tp2>
void convertTo(Mat_<_Tp2, chs>& _m, double alpha = 1, const Scalar& scalar = Scalar(0, 0, 0, 0)) const;

Mat_<_Tp, chs>& zeros(int _rows, int _cols);

// returns the matrix cols and rows
Size size() const;
// returns true if Mat_::total() is 0 or if Mat_::data is NULL
bool empty() const;
// returns the matrix element size in bytes: sizeof(_Tp) * channels
size_t elemSize() const;
// returns the size of each matrix element channel in bytes: sizeof(_Tp)
size_t elemSize1() const;
// returns the total number of array elements
size_t total() const;

// release memory
inline void release();
// destructor - calls release()
~Mat_() {

release(); };

public:
// the number of rows and columns
int rows, cols;
// channel num
int channels;
// pointer to the data
uchar* data;
// bytes per row
int step; // stride
// memory allocation flag
bool allocated;
// helper fields used in locateROI and adjustROI
const uchar* datastart;
const uchar* dataend;
}; // Mat_


const Mat_<uchar, 1>& mask = Mat_<uchar, 1>()


template<typename _Tp, int chs>
static int resize_cubic(const Mat_<_Tp, chs>& src, Mat_<_Tp, chs>& dst)


## 非模板类的模板构造函数

class any{

template <typename ValueType> any();
};


class any{

template <typename ValueType> any(ValueType a);
};


class any{

template <typename ValueType> any(const ValueType &);
};


any(const any &);

template <typename ValueType>
any(const ValueType &);


emmmm…我知道这一块的资料好乱。。。。。。

template<typename T1>
struct string {

// member template function
template<typename T2>
int compare(const T2&);
// constructors can be templates too
template<typename T2>
string(const std::basic_string<T2>& s) {

/*...*/ }
};
// out of class definition of string<T1>::compare<T2>
template<typename T1> // for the enclosing class template
template<typename T2> // for the member template
int string<T1>::compare(const T2& s) {

/* ... */ }


class Base {

virtual void f(int);
};
struct Derived : Base {

// this member template does not override Base::f
template <class T> void f(T);

// non-template member override can call the template:
void f(int i) override {

f<>(i);
}
};


## 模板成员函数

template<typename T>
struct A {

void f(int); // non-template member

template<typename T2>
void f(T2); // member template
};

//template member definition
template<typename T>
template<typename T2>
void A<T>::f(T2)
{

// some code
}

int main()
{

A<char> ac;
ac.f('c'); // calls template function A<char>::f<char>(int)
ac.f(1);   // calls non-template function A<char>::f(int)
ac.f<>(1); // calls template function A<char>::f<int>(int)
}