当前位置:网站首页>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
边栏推荐
- 阶段性总结与思考
- 這個flink cdc可以用在做oracle到mysql的,增量同步嗎
- MySQL 锁常见知识点&面试题总结
- Low code, end-to-end, one hour to build IOT sample scenarios, and the sound network released lingfalcon Internet of things cloud platform
- LeetCode85+105+114+124
- Unicom warehousing | all Unicom companies that need to sell their products need to enter the general warehouse first
- Ce CDC Flink peut - il être utilisé pour la synchronisation incrémentale d'Oracle à MySQL?
- Deep learning remote sensing data set
- 工业细节都是钱和时间砸出来的
- Daily mathematics serial 54: February 23
猜你喜欢

Vs2013 how to make the program run on other computers

Code sharing for making and developing small programs on the dating platform

文件操作的底层原理(文件描述符与缓冲区)
![[multithreading] how to implement timer by yourself](/img/a9/dd9489c7a0028dd9d3a6dae9a71deb.png)
[multithreading] how to implement timer by yourself

qt5.14.2连接ubuntu20.04的mysql数据库出错

联通入库|需要各地联通公司销售其产品的都需要先入总库

从零实现深度学习框架——LSTM从理论到实战【理论】

2022年第一季度保险服务数字化跟踪分析

Low code, end-to-end, one hour to build IOT sample scenarios, and the sound network released lingfalcon Internet of things cloud platform

VS2013如何让编写的程序在其它电脑上面也能运行
随机推荐
Which securities to choose for securities account opening? In addition, is it safe to open an account online now?
MySQL 锁常见知识点&面试题总结
22 years of a doctor in Huawei
科大讯飞 AI 学习机暑期新品发布会 AI + 教育深度结合再创产品新高度
Does rapid software delivery really need to be at the cost of security?
How to use filters in jfinal to monitor Druid for SQL execution?
Data mining review
mysql备份数据库linux
jfinal中如何使用过滤器监控Druid监听SQL执行?
5-2Web应用程序漏洞扫描
联通入库|需要各地联通公司销售其产品的都需要先入总库
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)
从零实现深度学习框架——LSTM从理论到实战【理论】
Go learning (IV. interface oriented)
LeetCode85+105+114+124
MooseFS的简介、部署及应用
新手必须知道的 Kubernetes 架构
MooseFS 调优笔记
MySQL backup and restore
ASP dynamically creates table table