当前位置:网站首页>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
边栏推荐
猜你喜欢

《天天数学》连载54:二月二十三日

为什么要同时重写hashcode和equals方法之简单理解

Mysql入库不了表情符号怎么办

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

Hidden worries behind the listing of shushulang: the performance has declined significantly, the market position is relatively backward, and the competitiveness is questionable

5 - 1 Analyse de vulnérabilité du système

Detailed explanation of MySQL and mvcc and the difference between RC and RR for snapshot reading
![Realizing deep learning framework from zero -- RNN from theory to practice [practice]](/img/a0/d64b69dec4a8f3a3dbc2eb47df9372.png)
Realizing deep learning framework from zero -- RNN from theory to practice [practice]

Dynamics 365online lookup lookup field multiple selection

2022 (第五届)GIS软件技术大会开幕,GIS、IT将加速融合
随机推荐
这个flink cdc可以用在做oracle到mysql的,增量同步吗
Simple understanding of why to rewrite hashcode and equals methods at the same time
Hezhou air32f103cbt6 development board hands-on Report
Day9 ---- 用户注册与登录
从第三次技术革命看企业应用三大开发趋势
Hidden worries behind the listing of shushulang: the performance has declined significantly, the market position is relatively backward, and the competitiveness is questionable
从零实现深度学习框架——LSTM从理论到实战【理论】
如果我在珠海,到哪里开户比较好?究竟网上开户是否安全么?
Ce CDC Flink peut - il être utilisé pour la synchronisation incrémentale d'Oracle à MySQL?
26 years old, 0 basic career change software test, from 3K to 16K monthly salary, a super complete learning guide compiled by me
[multithreading] how to implement timer by yourself
Detailed explanation of MySQL and mvcc and the difference between RC and RR for snapshot reading
软件快速交付真的需要以安全为代价吗?
文件操作的底层原理(文件描述符与缓冲区)
2022年第一季度保险服务数字化跟踪分析
What are the software testing methods and technical knowledge points?
Underlying principles of file operations (file descriptors and buffers)
leetcode:91. 解码方法【dfs + 记忆化】
Does rapid software delivery really need to be at the cost of security?
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)