c++ template 模板

目录

1. 模板是什么?

2.为什么需要模板

3.模板怎么用


和泛型搭配一起看

C++ Primer - 模板与泛型编程

你所不知道的 template

1. 模板是什么?

模板=函数模板+类模板

C++函数模板(Function Template)

C++类模板(Class Template)

        我们知道,数据的值可以通过函数参数传递,在函数定义时数据的值是未知的,只有等到函数调用时接收了实参才能确定其值。这就是值的参数化。
        在C++中,数据的类型也可以通过参数来传递,在函数定义时可以不指明具体的数据类型,当发生函数调用时,编译器可以根据传入的实参自动推断数据类型。这就是类型的参数化。
        值(Value)和类型(Type)是数据的两个主要特征,它们在C++中都可以被参数化。
        所谓函数模板,实际上是建立一个通用函数,它所用到的数据的类型(包括返回值类型、形参类型、局部变量类型)可以不具体指定,而是用一个虚拟的类型来代替(实际上是用一个标识符来占位),等发生函数调用时再根据传入的实参来逆推出真正的类型。这个通用函数就称为函数模板(Function Template)。
        在函数模板中,数据的值和类型都被参数化了,发生函数调用时编译器会根据传入的实参来推演形参的值和类型。换个角度说,函数模板除了支持值的参数化,还支持类型的参数化。
        一但定义了函数模板,就可以将类型参数用于函数定义和函数声明了。说得直白一点,原来使用 int、float、char 等内置类型的地方,都可以用类型参数来代替。

Generics With C++

Templates模板

        C++ implements generic programming concepts through templates. Templates give the compiler a framework to generate code for the types it gets implemented with. With a class, C++ will look at your template as well as the type specified when you created the object, and generate that typed class for you.

        C + + 通过模板实现了泛型的概念。模板为编译器提供了一个框架,用于为其实现的类型生成代码。对于类,c + + 将查看您的模板以及在创建对象时指定的类型,并为您生成类型化类。

        Think of it as a cookie cutter in the shape of a star. You can have various kinds of cookie dough, like shortbread, or chocolate-shortbread or chocolate chip cookie dough. The cookie cutter doesn’t care about the ingredients, as long as it is dough.

        可以把它想象成一个星形的饼干切割机。你可以有各种各样的曲奇面团,比如脆饼,或巧克力脆饼或巧克力片曲奇面团。只要是面团,饼干切割机就不会在意配料。

Generics in C++

        Generics can be implemented in C++ using Templates. Template is a simple and yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don’t need to write the same code for different data types. For example, a software company may need sort() for different data types. Rather than writing and maintaining the multiple codes, we can write one sort() and pass data type as a parameter.

        泛型可以使用模板在 c + + 中实现。模板是 c + + 中一个简单而又非常强大的工具。简单的想法是将数据类型作为参数传递,这样我们就不需要为不同的数据类型编写相同的代码。例如,软件公司可能需要 sort ()来处理不同的数据类型。与编写和维护多个代码不同,我们可以编写一个 sort ()并将数据类型作为参数传递。

C++ Templates

        Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

        模板是泛型的基础,它包括以独立于任何特定类型的方式编写代码。

        A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.

        模板是用于创建泛型类或函数的蓝图或公式。像迭代器和算法这样的库容器就是泛型的例子,并且是用模板概念开发的。

        There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>.

        每个容器都有一个单一的定义,例如 vector,但是我们可以定义许多不同类型的向量,例如 vector < int > 或 vector < string > 。

2.为什么需要模板

C++ Primer - 模板与泛型编程

不用模板:

        设计程序时往往希望同一个函数可以处理多种不同类型的参数,由于类型转换可能会引起各种各样的问题,我们需要对每一种所需的类型都重载一个几乎相同的函数。

int compare(const string &v1, const string &v2)
{
    if(v1 < v2) return -1;
    if(v2 < v1) return 1;
    return 0;
}
int compare(const double &v1, const double &v2)
{
    if(v1 < v2) return -1;
    if(v2 < v1) return 1;
    return 0;
}

用模板:

template <typename T>
int compare(const T &v1, const T &v2)
{
    if(v1 < v2) return -1;
    if(v2 < v1) return 1;
    return 0;
}

为什么需要模板

不用模板:

假定我们有很多个类型需要去实现相似的功能,比如,我们要实现不同类型的链表

不用模板的话大致有这样几种方法

  1. 枚举所有类型
  2. 使用void*

囿于篇幅只介绍一种利用宏来实现的,当然这种还是不建议使用

#define GetType(Type) Item##Type
#define Generate(Type)          \
struct GetType(Type) {          \   
    Type value;                 \
    struct GetType(Type) * next;\
}

Generate(int);
Generate(double);

int main() 
{
    GetType(int) il;
    il.value = 8;
    il.next = nullptr;  
    return 0;
}

用模板:

template <typename Type>
struct Item
{
    Type value;
    Item *next;
};
template <typename Type>
class List
{
public:
    ....
private:
    Item<Type> head_;
};
int main()
{
    List<int> il;
    List<double> dl;
    return 0;
}

3.模板怎么用

C++函数模板(Function Template)

C++类模板(Class Template)

C++中模板使用详解

上一篇:yaml J简介


下一篇:Nginx专题(1):Nginx之反向代理及配置