当前位置:网站首页>Advanced template (runner's notes)
Advanced template (runner's notes)
2022-07-26 19:03:00 【Just a garbage runner】
List of articles
Non type template parameters
There are two types of template parameters
Type template parameters : And follow class or typename The following parameter type name
Non type template parameters : Is to use a constant as a class ( function ) A parameter of the template , In the class ( function ) This parameter can be used as a constant in the template
there N Is constant , Is the existence of an initialization length that can be used as an array .

Be careful :
- Floating point numbers , Class objects and strings are not allowed as non type template parameters .
- Non type template parameters must be in the compiler to determine the result .
Template specialization
When using templates, we may need to deal with some fixed types, otherwise we will have problems with the code .
For example, we implement a less Function as a comparison of size .

At this point, you need to specialize the template .
And special function implementation operations for special types . Template specialization is divided into Class template specialization Function template specialization
among Function template specialization In fact, most of them can be solved by instantiation .
So we usually use template specialization on classes .
Full specialization
If there is the following code :
template<class T1,class T2>
class test
{
public:
test()
{
cout << "test()T1 T2" << endl;
}
};
template<>
class test<char,int>
{
public:
test()
{
cout << "test()char int" << endl;
}
};
int main()
{
test<int, int>();
test<char, int>();
return 0;
}

The execution result is shown in the figure above :
Here our test The class is realized through specialization when the class needs to be for example char int Type .
This specialization is called full specialization . And all template parameters are specialized .
Partial specialization
If there is the following code :
template<class T1,class T2>
class test
{
public:
test()
{
cout << "test()T1 T2" << endl;
}
};
Partial specialization can be divided into two kinds of specialization
- Partial specialization
template<class T1>
class test<T1 ,char>
{
public:
test()
{
cout << "test()T1 char" << endl;
}
};
The above code will be when T2 The type is char When I transferred .
- Type restriction specialization
We can also specify a type for specialization, such as The pointer , quote .
The code is as follows :
template<class T1, class T2>
class test<T1*,T2*>// Specialization of pointer
{
public:
test()
{
cout << "test()<T1*,T2*>" << endl;
}
};
template<class T1, class T2>// Specialization of reference types
class test<T1&, T2&>
{
public:
test()
{
cout << "test<T1&, T2&>" << endl;
}
};
template<class T1, class T2>// Reference plus pointer
class test<T1&, T2*>
{
public:
test()
{
cout << "test<T1&, T2*>" << endl;
}
};
边栏推荐
- 手机申请公募reits账户安全吗?
- 2022年云商店联合营销市场发展基金(MDF)介绍
- [AUTOSAR RTE] - 1-talk about RTE (run time environment)
- Development of NFT digital collection system: Shanxi first released digital collections of ancient buildings on "China Tourism Day"
- 2022茶艺师(中级)考试题模拟考试题库及答案
- 微软默默给 curl 捐赠一万美元,半年后才通知
- 【在 Kotlin 中添加条件行为】
- FTP protocol
- Have you ever encountered a deadlock problem in MySQL? How did you solve it?
- CTO will teach you: how to take over his project when a technician suddenly leaves
猜你喜欢

SMMU carding

MySQL练习题初级45题(统一表)

2022 Shanghai safety officer C certificate operation certificate examination question bank simulated examination platform operation

【考研词汇训练营】Day 13 —— reliance,expert,subject,unconscious,photograph,exaggeration,counteract

JS刷题计划——链表

Operations research 69 | explanation of classic examples of dynamic planning

Simulated 100 questions and simulated examination of refrigeration and air conditioning equipment operation examination in 2022

一文详解MES系统给企业带来的5大好处,附应用场景

SSM整合-异常处理器和项目异常处理方案

微软默默给 curl 捐赠一万美元,半年后才通知
随机推荐
flex布局
Racher deploys kubernetes cluster
How to become an excellent test / development programmer? Focus on planning and then move
SSM整合-异常处理器和项目异常处理方案
微软默默给 curl 捐赠一万美元,半年后才通知
Interview summary of some large factories
MySQL学习笔记-2.如何提高sql语句的查询性能
我酷故我在
ALV screen input option learning
还在用Xshell?推荐这个更现代的终端连接工具
分布式事务-seata
2022G1工业锅炉司炉上岗证题库及模拟考试
2022 cloud store joint marketing development fund (MDF) Introduction
MES系统最全介绍来了
JS map usage
The principle of database index, why use b+ tree, why not binary tree?
NFT digital collection system development: fellow uncle first promoted the blessing series digital collection, which will be sold out immediately
Flex layout
【MySQL从入门到精通】【高级篇】(八)聚簇索引&非聚簇索引&联合索引
JS question brushing plan - linked list


