当前位置:网站首页>Efficient implementation of dynamiccast with template function and specialization function
Efficient implementation of dynamiccast with template function and specialization function
2022-06-29 22:31:00 【kupeThinkPoem】
Catalog
3、 ... and 、 Template specialization
Four 、dynamicCast Template implementation of
5、 ... and 、 Reference resources
One 、 summary
A template is a parameterized polymorphic tool So called parameterized polymorphism , Refers to the processing of the program Object type parameterization , So that a piece of program code can be used to deal with many different types of objects . Using template programming , It can provide a code sharing mechanism for various programs with the same logic function and different data types . Templates include function templates (function template)、 Class template (class template). This article mainly discusses function templates
Two 、 template function
Function templates provide a unique piece of function code for all functions , Enhance the versatility of function design . The method of using function template is to explain the function template first , Then instantiate the corresponding template function to call and execute Function templates are not functions , Cannot be executed to replace the type parameter in the code to get the template function —— Instantiation , The instantiated template function is a real function , Can be executed .
Before instantiation , First check the template code itself , Check whether the syntax is correct ; Syntax errors will be found here , If you omit a semicolon, etc . During instantiation , Check the template code , See if all calls are valid . Invalid calls will be found here , For example, the instantiation type does not support some function calls or operators . Ordinary functions only need to declare , You can compile successfully , For template compilation, you need to view the definition of the template ( The declaration and definition should be placed in the same .h file ).
3、 ... and 、 Template specialization
1、 Full specialization
template <class T1,class T2>
void calc(T1 a, T2 a)
{
}
template <>
void calc(double a, double a)
{
}2、 Local specialization
(1)、 Local specialization parameter
template <class T1,class T2>
void calc(T1 a, T2 b)
{
}
template <>
void calc(T1 a, double b)
{
}(2)、 Local specialization to pointer type
template <class T1,class T2>
void calc(T1 a, T2 b)
{
}
template <class T1,class T2>
void calc(T1* a, T2 *b)
{
}(3) Example
Suppose we now have such a template function max:
template <typename T> const T& max(const T& a, const T& b) {
return a < b ? b : a;
}And now we're going to compare the size of the two strings , Such as :
const char* str1 = "ttt"; const char* str2 = "ccc";At this point, if you follow the general instantiation , The comparison will be str1 and str2 Size , That is, compare the pointer value size , Not string size , So we need to implement the specialization of a template function , as follows :
template<> const char* const& max(const char* const& a, const char* const& b) {
return strcmp(a, b) < 0 ? b : a;
}Four 、dynamicCast Template implementation of
class A
{
public:
virtual ~A(){}
virtual int getType()
{
return 0;
}
};
class B:public A
{
public:
virtual int getType()
{
return 1;
}
};
template <class Target, class Source>
inline Target* dynamicCast(Source* x)
{
Target tmp = dynamic_cast<Target>(x);
return tmp;
}
template<>
inline B* dynamicCast(A* x)
{
if(x->getType()==1)
{
return (B*)(x);
}
return NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
A* a=new B();
B*b=dynamicCast<B>(a);
delete a;
return 0;
}
5、 ... and 、 Reference resources
边栏推荐
- The correct method for Navicat to connect to mysql8.0 (valid for personal testing)
- A mysql IBD file is too large processing record
- LeetCode85+105+114+124
- 正如以往我们对于互联网的看法一样,我们对于互联网的认识开始变得深度
- 5-2Web应用程序漏洞扫描
- Three development trends of enterprise application viewed from the third technological revolution
- 22 years of a doctor in Huawei
- 阶段性总结与思考
- math_ Basic elementary function graph (power function / exponent / logarithm / trigonometry / inverse trigonometry)
- Grep工具
猜你喜欢

华为云AOM 2.0版本发布

便携式4K音视频会议终端一体机带8倍数字变焦

2022 (第五届)GIS软件技术大会开幕,GIS、IT将加速融合

5-1系统漏洞扫描

Three development trends of enterprise application viewed from the third technological revolution

数论-整除分块

把数组排成最小的数_数组中的逆序对(归并统计法)_数字在升序数组中出现的次数_丑数(剑指offer)

Matplotlib histogram

Cout ambiguous problem

Arrange the array into the smallest number_ Reverse pairs in an array (merge Statistics)_ Number of occurrences of a number in an ascending array_ Ugly number (Sword finger offer)
随机推荐
26岁,0基础转行软件测试,从月薪3k到16k,我整理的超全学习指南
低代码、端到端,一小时构建IoT示例场景,声网发布灵隼物联网云平台
LeetCode85+105+114+124
从零实现深度学习框架——LSTM从理论到实战【理论】
合宙AIR32F103CBT6开发板上手报告
Build a short video platform, fade in and fade out, and support left sliding and right pulley to broadcast pictures
Digital tracking analysis of insurance services in the first quarter of 2022
Underlying principles of file operations (file descriptors and buffers)
华为7年经验的软件测试总监,给所有想转行学软件测试的同学的几个建议
Three development trends of enterprise application viewed from the third technological revolution
Introduction, deployment and application of moosefs
Data mining review
研发测试时间比,BUG数据分析
为什么要同时重写hashcode和equals方法之简单理解
Dynamics 365online lookup lookup field multiple selection
英语没学好到底能不能做coder,别再纠结了先学起来
便携式4K音视频会议终端一体机带8倍数字变焦
Detailed description of gaussdb (DWS) complex and diverse resource load management methods
云原生爱好者周刊:炫酷的 Grafana 监控面板集合
Deep learning remote sensing data set