当前位置:网站首页>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;
}
};
边栏推荐
- 2022T电梯修理考试题及在线模拟考试
- Duplicate gallerycms character length limit short domain name bypass
- What should we do after the PMP Exam is postponed on July 30?
- JS使用readline来实现终端输入数据
- NFT数字藏品系统开发:同道大叔首推祈福系列数字藏品开售即罄
- The United States, Japan and South Korea jointly developed 6G with the intention of anti surpassing, but China has long been prepared
- Visual VM positioning oom, fullgc usage
- [AUTOSAR RTE] - 1-talk about RTE (run time environment)
- 模型定义#pytorch学习
- MySQL exercises elementary 45 questions (Unified table)
猜你喜欢

SSM integration - exception handler and project exception handling scheme
![[soft exam] soft exam tutorial + real questions over the years](/img/91/72cdea3eb3f61315595330d2c9016d.png)
[soft exam] soft exam tutorial + real questions over the years

SSM integration - functional module and interface testing

FTP protocol

2022T电梯修理考试题及在线模拟考试

VTK (the Visualization Toolkit) loads STL models

NFT digital collection development: digital collections help enterprise development

Tensor RT's int8 quantization principle

Interview summary of some large factories

我酷故我在
随机推荐
Arrangement of information security emergency plan
SSM integration configuration
手机申请公募reits账户安全吗?
【考研词汇训练营】Day 13 —— reliance,expert,subject,unconscious,photograph,exaggeration,counteract
2022年焊工(初级)操作证考试题库及模拟考试
NFT数字藏品系统开发:同道大叔首推祈福系列数字藏品开售即罄
CTO will teach you: how to take over his project when a technician suddenly leaves
JS question brushing plan - array
2022茶艺师(中级)考试题模拟考试题库及答案
分布式事务-seata
JS刷题计划——数组
MySQL - function and constraint commands
2022G1工业锅炉司炉上岗证题库及模拟考试
rancher部署kubernetes集群
Racher deploys kubernetes cluster
Offer set (1)
Development of NFT digital collection system: Shanxi first released digital collections of ancient buildings on "China Tourism Day"
Unity 农场 2 —— 种植系统
MySQL - 多表查询与案例详解
How to become an excellent test / development programmer? Focus on planning and then move


