C++ 中的模板

类模板

template <typename T>
class A{
private:
    T x;
};

函数模板

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

成员模板(member template)

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

来看这个例子:

#include <iostream>
#include <vector>
#include <algorithm>
 
struct Printer { // generic functor
    std::ostream& os;
    Printer(std::ostream& os) : os(os) {}
    template<typename T>
    void operator()(const T& obj) { os << obj << ' '; } // member template
};
 
int main()
{
    std::vector<int> 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 <iostream>
#include <algorithm>

using std::cout;

template <typename T>
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<typename T, typename Alloc=...>
class vector
{
  ...
}

特化第一个 typename:

template<typename Alloc=...>
class vector<bool, Alloc>
{
  ...
}

范围的偏(partial specialization)

template <typename T>
class C
{
  ...
}

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

template <typename T>
class C<T*>
{
  ...
}

具体用法就是:

C<string> obj1;
C<string*> obj2;

模板模板参数

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

class A{
private:
    std::list<int> l1;
};

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

那么我们可以这么写:

template <typename T>
class A{
private:
    std::list<T> l1;
};

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

那么我们就可以这样写:

#include <iostream>
#include <list>

template<typename T, template<typename U, typename Alloc>class Container>
class A{
private:
    Container<T, std::allocator<T>> c;
};

int main() {

    A<int, std::list> a;

    return 0;
}

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

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

template <class T, class Sequence = std::deque<T>>
class A{
    
};

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

A<int> a;
A<int, std::list<int>> a;

参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容