C++ 中的模板

类模板

template 
class A{
private:
    T x;
};

函数模板

template 
inline
const T& my_min(T& x, T& y) {
    return x < y ? x : y;
}

成员模板(member template)

成员模板其实就是一个类里面使用了一个模板函数。使用模板函数的时候是不需要指定参数类型的

来看这个例子:

#include 
#include 
#include 
 
struct Printer { // generic functor
    std::ostream& os;
    Printer(std::ostream& os) : os(os) {}
    template
    void operator()(const T& obj) { os << obj << ' '; } // member template
};
 
int main()
{
    std::vector v = {1,2,3};
    std::for_each(v.begin(), v.end(), Printer(std::cout));
    std::string s = "abc";
    std::for_each(s.begin(), s.end(), Printer(std::cout));
}

结果:

1 2 3 a b c 

模板特化(specialization)

其实是对模板的某一种特定类型的解释,比如下面这个例子:

#include 
#include 

using std::cout;

template 
size_t hash(const T& x){
    return 0;
}

template <>
size_t hash(const char& x){
    return x;
}

int main() {
    char x1 = 'a';
    int x2 = 3;

    cout << "x1(char) hash: " << hash(x1) << '\n';
    cout << "x2(int) hash: " << hash(x2) << '\n';
}

模板偏特化

个数的偏

对于有多个模板参数,我们可以选择一个或者多个指定类型。比如看到 vector 的实现:

template
class vector
{
  ...
}

特化第一个 typename:

template
class vector
{
  ...
}

范围的偏(partial specialization)

template 
class C
{
  ...
}

可以指定 T 是指针,应该怎么做:

template 
class C
{
  ...
}

具体用法就是:

C obj1;
C obj2;

模板模板参数

这个比较难理解,我先举这样一个例子。假如我们需要这样一个类,它的成员变量有一个 std::list,list 的类型是 int。那么我们可以这样写:

class A{
private:
    std::list l1;
};

那这个类能不能更通用化呢?list 的类型可以由用户指定呢?

那么我们可以这么写:

template 
class A{
private:
    std::list l1;
};

再通用化一点,把 list 换成任意容器,且这个容器存储的东西也由用户指定。

那么我们就可以这样写:

#include 
#include 

templateclass Container>
class A{
private:
    Container> c;
};

int main() {

    A a;

    return 0;
}

其中上面第二个模板参数就是模板模板参数。

再看下面这个是不是模板模板参数:

template >
class A{
    
};

这个就不是了,我们在使用的过程中要指定具体的类型,而不是模板。比如:

A a;
A> a;

参考

  • https://stackoverflow.com/questions/213761/what-are-some-uses-of-template-template-parameters

你可能感兴趣的:(C++ 中的模板)