Template content learning notes (1)

1. Define the template

16.1.1 Function Template

We can define a general function template instead of defining a new function for each type

We can do this:

#include <memory>
#include <cstring>

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

    if(v1>v2)
    {
        return 1;
    }

    return 0;
}

int main()
{
    printf("%d\n",compire(1,2));
    return 0;
}

 

 

 

 

Class template

We found that our template has a parameter called T

//
// Created by root on 20-2-27.
//

#ifndef TEST_TEMPLATE_H
#define TEST_TEMPLATE_H
//
// Created by root on 20-2-27.
//
#include <vector>
template <typename T>class BlobA{

public:
    typedef T value_type;
    typedef typename std::vector<T>::size_type  size_type;
    BlobA()
    {

    }
    int add(T a,T b)
    {
        return a+b;
    }
};
#endif //TEST_TEMPLATE_H

Calling place

#include <memory>
#include <cstring>
#include <iostream>
#include "template.h"
int main()
{
    BlobA<int> a;
    std::cout<<a.add(1,2)<<std::endl;
    return 0;
}

Instantiate class template:

We have seen many times that when using a class template, we must provide additional information to display a list of template arguments, which are bound to template parameters. The compiler uses these template arguments to instantiate these specific classes.

Seeing the book here is a demo written like this, which aroused my doubts

#include <memory>
#include <cstring>
#include <iostream>
#include "template.h"
int main()
{
    BlobA<int> a = {0,1,2};
    std::cout<<a.add(1,2)<<std::endl;
    return 0;
}

Why class can be initialized to {0,1,2}

The original constructor is overloaded here

//
// Created by root on 20-2-27.
//

#ifndef TEST_TEMPLATE_H
#define TEST_TEMPLATE_H
//
// Created by root on 20-2-27.
//
#include <vector>
template <typename T>class BlobA{

public:
    typedef T value_type;
    typedef typename std::vector<T>::size_type  size_type;
    BlobA()
    {

    }
    BlobA(std::initializer_list<T> il)
    {

    }
    int add(T a,T b)
    {
        return a+b;
    }
};
#endif //TEST_TEMPLATE_H

Here is a brief talk about using initializer_list for custom types

C++11 also provides methods for users to use initialization lists in custom types (generally referred to as classes).

Let's take a look at the instantiated class

BlobA(std::initializer_list il)
{

    for (auto a : il)
    {
        std::cout<<a<<std::endl;
    }
}

The result is 0,1,2,3

We continue to read the back of the book

Smart pointer usage template

std::shared_ptr<BlobA<int>> data;

data is an instance of shared_ptr, this shared_ptr holds a vector instance of an object of type T

1. Define the template

16.1.1 Function Template

We can define a general function template instead of defining a new function for each type

We can do this:

#include <memory>
#include <cstring>

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

    if(v1>v2)
    {
        return 1;
    }

    return 0;
}

int main()
{
    printf("%d\n",compire(1,2));
    return 0;
}

Class template

We found that our template has a parameter called T

//
// Created by root on 20-2-27.
//

#ifndef TEST_TEMPLATE_H
#define TEST_TEMPLATE_H
//
// Created by root on 20-2-27.
//
#include <vector>
template <typename T>class BlobA{

public:
    typedef T value_type;
    typedef typename std::vector<T>::size_type  size_type;
    BlobA()
    {

    }
    int add(T a,T b)
    {
        return a+b;
    }
};
#endif //TEST_TEMPLATE_H

Calling place

#include <memory>
#include <cstring>
#include <iostream>
#include "template.h"
int main()
{
    BlobA<int> a;
    std::cout<<a.add(1,2)<<std::endl;
    return 0;
}

Instantiate class template:

We have seen many times that when using a class template, we must provide additional information to display a list of template arguments, which are bound to template parameters. The compiler uses these template arguments to instantiate these specific classes.

Seeing the book here is a demo written like this, which aroused my doubts

#include <memory>
#include <cstring>
#include <iostream>
#include "template.h"
int main()
{
    BlobA<int> a = {0,1,2};
    std::cout<<a.add(1,2)<<std::endl;
    return 0;
}

Why class can be initialized to {0,1,2}

The original constructor is overloaded here

//
// Created by root on 20-2-27.
//

#ifndef TEST_TEMPLATE_H
#define TEST_TEMPLATE_H
//
// Created by root on 20-2-27.
//
#include <vector>
template <typename T>class BlobA{

public:
    typedef T value_type;
    typedef typename std::vector<T>::size_type  size_type;
    BlobA()
    {

    }
    BlobA(std::initializer_list<T> il)
    {

    }
    int add(T a,T b)
    {
        return a+b;
    }
};
#endif //TEST_TEMPLATE_H

Here is a brief talk about using initializer_list for custom types

C++11 also provides methods for users to use initialization lists in custom types (generally referred to as classes).

Let's take a look at the instantiated class

BlobA(std::initializer_list il)
{

    for (auto a : il)
    {
        std::cout<<a<<std::endl;
    }
}

The result is 0,1,2,3

We continue to read the back of the book

Smart pointer usage template

std::shared_ptr<BlobA<int>> data;

data is an instance of shared_ptr, this shared_ptr holds a vector instance of an object of type T

Member functions of class templates

We can define member functions both inside and outside the class template, and the member functions defined in the template are implicitly declared as introverted functions.

Class template member functions defined outside the template must start with template

As always, when we define a member function outside a class, we must specify which class the member belongs to. And the name of a template generation class must include its template arguments. When we define a member function, we must specify which class the member belongs to. And the name of the class generated from a template must contain the actual parameters of the template. When we define a member function, the template actually participates in the same template parameter. For a member function, the member corresponding to the blob should look like this.

template <typename T>
ret-type Blob<T>::member-name(parm-list)

Note that the definition and declaration of the template cannot be separated into two files, because the specific class will not be generated when compiling, only when it is called, so you need to include .cc, but this is not a good solution, so it is still written in A .h file is better

For example, one is placed in .h and one is placed in .cc

The most common form of a friendly relationship between a class template and another template is to establish a friendly relationship between instances and friends

//
// Created by root on 20-2-27.
//

#ifndef TEST_TEMPLATE_H
#define TEST_TEMPLATE_H
#include <vector>
#include <iostream>
// Created by root on 20-2-27.
//

template <typename >class BlobPtr;
template <typename >class Blob;

template <typename T>
bool operator == (const Blob<T>&,const Blob<T>&);
template <typename T>class BlobA{

public:
    friend class BlobPtr<T>;
    friend bool operator == <T>(const Blob<T>&,const Blob<T>&);
    typedef T value_type;
    typedef typename std::vector<T>::size_type  size_type;
    BlobA()
    {

    }
    BlobA(std::initializer_list<T> il)
    {

        for (auto a : il)
        {
            std::cout<<a<<std::endl;
        }
    }
    int add(T a,T b);

};

template <typename T>
int BlobA<T>::add(T a, T b) {
    return a+b;
}
#endif //TEST_TEMPLATE_H

Template type alias

The characteristics of the template type, typedef BlobA data2;

another way

Want to specify the alias is not an instantiated template class, but a template

template <typename T>using partNo = BlobA<unsigned>;

Static member of class template

As with any other class, class templates can also declare static members.

template <typename T>class BlobA{
public:

    static std::size_t count(){ return ctr;};

    static std::size_t ctr;

private:
};

//初始化
template <typename T>size_t BlobA<T>::ctr = 0;

//使用
int main()
{

    std::cout<<partNo<int>::count()<<std::endl;
    return 0;
}

 

 

Member functions of class templates

We can define member functions both inside and outside the class template, and the member functions defined in the template are implicitly declared as introverted functions.

Class template member functions defined outside the template must start with template

As always, when we define a member function outside a class, we must specify which class the member belongs to. And the name of a template generation class must include its template arguments. When we define a member function, we must specify which class the member belongs to. And the name of the class generated from a template must contain the actual parameters of the template. When we define a member function, the template actually participates in the same template parameter. For a member function, the member corresponding to the blob should look like this.

template <typename T>
ret-type Blob<T>::member-name(parm-list)

Note that the definition and declaration of the template cannot be separated into two files, because the specific class will not be generated when compiling, only when it is called, so you need to include .cc, but this is not a good solution, so it is still written in A .h file is better

For example, one is placed in .h and one is placed in .cc

The most common form of a friendly relationship between a class template and another template is to establish a friendly relationship between instances and friends

//
// Created by root on 20-2-27.
//

#ifndef TEST_TEMPLATE_H
#define TEST_TEMPLATE_H
#include <vector>
#include <iostream>
// Created by root on 20-2-27.
//

template <typename >class BlobPtr;
template <typename >class Blob;

template <typename T>
bool operator == (const Blob<T>&,const Blob<T>&);
template <typename T>class BlobA{

public:
    friend class BlobPtr<T>;
    friend bool operator == <T>(const Blob<T>&,const Blob<T>&);
    typedef T value_type;
    typedef typename std::vector<T>::size_type  size_type;
    BlobA()
    {

    }
    BlobA(std::initializer_list<T> il)
    {

        for (auto a : il)
        {
            std::cout<<a<<std::endl;
        }
    }
    int add(T a,T b);

};

template <typename T>
int BlobA<T>::add(T a, T b) {
    return a+b;
}
#endif //TEST_TEMPLATE_H

Template type alias

The characteristics of the template type, typedef BlobA data2;

another way

Want to specify the alias is not an instantiated template class, but a template

template <typename T>using partNo = BlobA<unsigned>;

Static member of class template

As with any other class, class templates can also declare static members.

template <typename T>class BlobA{
public:

    static std::size_t count(){ return ctr;};

    static std::size_t ctr;

private:
};

//初始化
template <typename T>size_t BlobA<T>::ctr = 0;

//使用
int main()
{

    std::cout<<partNo<int>::count()<<std::endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_32783703/article/details/104583871