当前位置:网站首页>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;
}
};
边栏推荐
- rancher部署kubernetes集群
- JS question brushing plan - linked list
- 模型定义#pytorch学习
- Accused of excessive patent licensing fees! The U.S. Court ruled that Qualcomm violated the antitrust law: Qualcomm's share price fell 10.86%!
- CTO will teach you: how to take over his project when a technician suddenly leaves
- Safer, healthier and without endurance anxiety, Wei brand latte dht-phev is here
- NFT digital collection system development: sold out when online, and netizens "spike" Digital Collections
- 2022 chemical automation control instrument test question simulation test platform operation
- Flex layout
- 14. Gradient detection, random initialization, neural network Summary
猜你喜欢
随机推荐
JS刷题计划——链表
JS map usage
基础模块及算例#pytorch学习
Interview summary of some large factories
Automated test tool playwright (quick start)
NFT数字藏品开发:数字藏品助力企业发展
Still using xshell? Recommend this more modern terminal connection tool
How to become an excellent test / development programmer? Focus on planning and then move
MES系统的选择需重点考虑哪些方面?
VTK (the Visualization Toolkit) loads STL models
2022年焊工(初级)操作证考试题库及模拟考试
MySQL - multi table query and case explanation
此章节用于补充3
Concentrate, heart to heart! The Chinese funded mobile phone Enterprises Association (CMA) of India is officially operational!
Meta Cambria handle exposure, active tracking + multi tactile feedback scheme
2022年化工自动化控制仪表考题模拟考试平台操作
14. Gradient detection, random initialization, neural network Summary
CoVOS:无需解码!利用压缩视频比特流的运动矢量和残差进行半监督的VOS加速(CVPR 2022)...
SSM integration - exception handler and project exception handling scheme
NFT数字藏品系统开发:上线即售罄,网民“秒杀”数字藏品












