理解template In C++ :第一重

  • shared_ptr

template <class T> class shared_ptr;

shared_ptr 定义如上,是一个template,具体应该怎么理解呢?

  • template

wikipedia

cplusplus

geeksforgeeks

tutorialspoint

cppreference

From wikipedia, Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.

There are three kinds of templates: function templates, class templates, variable templates(since C++14).

  • Function templates

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

The format for declaring function templates with type parameters is :

template <class identifier> function_declaration;
template <typename identifier> function_declaration;

To create a template function that returns the greater one of two objects we could use:

template <class myType>
myType GetMax (myType a, myType b){
    return (a>b?a:n);
}

Here we have created a template function with myType as its template parameters. This template parameter represents a type that has not yet been specified, but that can be used in the template function as if it were regualr type.

To use this function template we use the following format for the function call:

function_name <type> (parameters);

For example:

int x, y;
GetMax <int> (x,y);
  • Class templates

We also have the possibility to write class templates, so that a class can have members that use template parameters as types.

template <class T>
class mypair {
    T value [2];
  public:
    mypair (T first, T second)
    {
        values[0]=first; value[1]=second;
    }
};

The class that we have just defined serves to store two elements of any valid type. For example, if we wanted to declare an object of this class to store two integer values of type int with the values 115 and 36 we would write:

mypair <int> myobject(115, 36);

This same class would also be used to create an object to store any other type:

mypair <double> myfloats (3.0, 2.18);
  • Template specialization

Template specialization allows us to have different code for a particualr data type.

It is possible in C++ to get a special behavior for a particular data type. This is called template specialization.

When we write any template based function or class, compiler creates a copy of that function/class whenever compiler sees that being used for a new data type or new set of data types(in case of multiple template arguments).

If a specialized version is present, compiler first checks with the specialized version and then the main template. Compiler first checks the most specialized version by matching the passed parameter with the data type(s) specified in a specialized version.

  • Non-type parameters for templates
  • Templates and multiple-file projects
  • How templates work

Templates are expanded at compiler time. This is like macros.

The difference is , compiler does type checking before template expansion.

The idea is simple, source code contains only function/class, but comiled code may contain multiple copies of same function/class.

  • FAQ on template

  1. What is the difference between function overloading and templates?

    Both are examples of polymorphism feature of OOP.

    Function overloading is used when multiple functions do similar operations;

    Templates are used when mutiple functions do identical operations.

  2. What happens when there is static member in a template class/function?

    Each instance of a tempalte contains its own static vairable.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值