一、类型萃取

笔者目前以这种使用场景来解释什么是类型萃取,为什么需要类型萃取?看代码会更加明了。
Demo.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef DEMO_H
#define DEMO_H

//为什么需要萃取器?

//自设计的模板Object<_Ty>类型
template<class _Ty>
class Object
{
private:
_Ty value;
public:
Object(_Ty val = 0) : value(val) {}
~Object() {}

const _Ty Value()const
{
return value;
}
_Ty Value()
{
return value;
}
};

//自设计模板容器Container<_Container>类型
template<class _Container>
class Container
{
private:
_Container c;
public:
Container(_Container x = 0) : c(x)
{}

//例如Container<Object<int>> con;
//con容器中存放的是Object<int>类型的对象
//此时通过con.getObjectValue()得到存放对象的方法

返回值类型 ? ? getObjectValue()
{
//此时并不知道容器中对象的成员方法的返回值类型
return c.Value();
}
};
#endif

通过这个场景可以看出,我们非常有必要知道c.Value()的返回值类型,若是在Object类中,我们很容易知道返回值类型就是_Ty,但是经过一次封装后,通过封装的Container类是不能直接去获得存储在其中的对象的成员中的类型。那么类型萃取就诞生了。(不仅仅局限于这个原因,更重要C++追求效率的缘故,这个后面再说)

那我们加上类型萃取器(类)后:
Demo.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef DEMO_H
#define DEMO_H

//为什么需要萃取器?

//自设计的模板类型
template<class _Ty>
class Object
{
private:
_Ty value;
public:
//第一步:对需要萃取的类型进行类型重命名
//这里需要_Ty的类型
typedef _Ty value_type; //value值类型

Object(_Ty val = 0) : value(val) {}
~Object() {}

const _Ty Value()const
{
return value;
}
_Ty Value()
{
return value;
}
};

//Object类的类型萃取器:
/*
* 当使用Object<int>类型推演时
* 该类中将Object<int>::value_type进行类型重命名value_type
* 最终在其他地方使用Object_traits<_con>
*/
template<class _Object>
struct Object_traits
{
typedef typename _Object::value_type value_type;
//... 需要什么类型就typedef 什么就行
};

//自设计模板容器类型
template<class _Container>
class Container
{
private:
_Container c;
public:
Container(_Container x = 0) : c(x)
{}
//typename说明value_type是Object_traits中的一个类型
typename Object_traits<_Container>::value_type getObjectValue()
{
return c.Value();
}
};
#endif

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "Demo.h"
using namespace std;
int main()
{
Object<int> iobj(10);
Container<Object<int>> icon(iobj);
cout << icon.getObjectValue() << endl;

Object<double> dobj(3.14);
Container<Object<double>> dcon(dobj);
cout << dcon.getObjectValue() << endl;

return 0;
}

运行结果:
在这里插入图片描述
类型萃取时机总结:

  • 模板类A作为模板类B的模板参数,并且模板B类中需要使用模板类A中的模板参数类型

二、通过类型萃取实现函数重载

通过这几种组合起来,我们能达到早绑定的效果:在编译时期就确定函数的调用时机。

Demo.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef DEMO_H
#define DEMO_H
#include <iostream>
using namespace std;

//自设计的模板类型
template<class _Ty>
class Object
{
private:
_Ty value;
public:
//第一步:对需要萃取的类型进行类型重命名
//这里需要_Ty的类型
typedef _Ty value_type; //value值类型

Object(_Ty val = 0) : value(val) {}
~Object() {}

const _Ty Value()const
{
return value;
}
_Ty Value()
{
return value;
}
};

//Object类的类型萃取器:
/*
* 当使用Object<int>类型推演时
* 该类中将Object<int>::value_type进行类型重命名value_type
* 最终在其他地方使用Object_traits<_con>
*/
template<class _Object>
struct Object_traits
{
typedef typename _Object::value_type value_type;
//... 需要什么类型就typedef 什么就行
};


//模板函数:可以作为其他(模板)函数重载的条件
template<class _OBj>
typename Object_traits<_OBj>::value_type dif_Condition(const _OBj&)
{
//返回该类的临时对象
return typename Object_traits<_OBj>::value_type();
}

//自设计模板容器类型
template<class _Container>
class Container
{
private:
_Container c;
public:
Container(_Container x = 0) : c(x)
{}
//typename说明value_type是Object_traits中的一个类型
typename Object_traits<_Container>::value_type getObjectValue()
{
return c.Value();
}
private:
//两个重载的函数
void __fun(int)
{
cout << " int "<< endl;
}
void __fun(double)
{
cout << " double " << endl;
}
public:
void fun()
{
//通过dif_condition的返回值(对象)的类型进行不同函数的调用
__fun(dif_Condition(c));
}
};
#endif

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "Demo.h"
using namespace std;
int main()
{
Object<int> iobj(10);
Container<Object<int>> icon(iobj);
cout << icon.getObjectValue() << endl;
icon.fun();

Object<double> dobj(3.14);
Container<Object<double>> dcon(dobj);
cout << dcon.getObjectValue() << endl;
dcon.fun();
return 0;
}

测试结果:
在这里插入图片描述

本节主要为了充分理解类型粗萃取的原理,后面将会对STL中的迭代器进行论述。