ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 泛型编程组件(转)

泛型编程组件(转)

原创 Linux操作系统 作者:jcszjswkzhou 时间:2019-03-29 21:30:07 0 删除 编辑
泛型编程组件
下面将介绍泛型编程的最重要的组件,他们是:
1。编译期断言
2。编译期转换检测
3。编译期选择
4。编译期多态
5。代码容器

再一次说,这是泛型里最重要的组件,值得你去理解和使用,他们的实现有很多版本,Boost和Loki都有实现,在认真比较以后,我认为Loki确实更加简单和强大,所以我选择Loki的代码进行示例,如果你有兴趣深入了解,请读《C++设计新思维》。

值得注意的是,为了便于理解,我把Loki的代码进行了适当修改。

1。编译期断言

实现:
template struct CompileTimeError;
template<> struct CompileTimeError {};

#define STATIC_CHECK(expr, msg)
{ CompileTimeError<((expr) != 0)> ERROR_##msg; (void)ERROR_##msg; }

原理:
利用template<> struct CompileTimeError并没有特化体,所以产生他的实例ERROR_##msg编译器就会报错。

使用:
const int i = 0;
STATIC_CHECK(i==0, "i因该等于0");


2。编译期转换检测

实现:
template
struct ConversionHelper
{
typedef char Small;
struct Big { char dummy[2]; };
static Big Test(...);
static Small Test(U);
static T MakeT();
};

template
struct Conversion
{
typedef ConversionHelper H;
enum { exists = sizeof(typename H::Small) == sizeof(H::Test(H::MakeT())) };
enum { exists2Way = exists && Conversion::exists };
enum { sameType = false };
};

template
struct Conversion
{
enum { exists = 1, exists2Way = 1,sameType = 1 };
};

template
struct Conversion
{
enum { exists = 1, exists2Way = 0,sameType = 0 };
};

template
struct Conversion
{
enum { exists = 1, exists2Way = 0,sameType = 0 };
};

template <>
class Conversion
{
public:
enum { exists = 1, exists2Way = 1,sameType = 1 };
};

#define SUPERSUBCLASS(T, U)
(Conversion::exists &&
!Conversion::sameType)

#define SUPERSUBCLASS_STRICT(T, U)
(SUPERSUBCLASS(T, U) &&
!Conversion::sameType)

原理:
利用函数的参数列表的型别推测机制得到编译器使用哪个函数,而函数返回型别,接着利用 sizeof()一定会在编译期求出值来获得返回型别的大小,依此来判断调用了哪个函数,然后在利用enum是编译期确定的常数,把sizeof()返回的值具体化出来。

使用:
class A { };
class B : public A { };

STATIC_CHECK( SUPERSUBCLASS(A, B), "A因该是B的基类");


3。编译期选择

实现:
template
struct Select
{
typedef T Result;
};
template
struct Select
{
typedef U Result;
};

原理:
模板偏特化

使用:
class A { };
class B { };

template
class C : public Select { };


4。编译期多态

实现:
template
class Relex
{
private:
Relex(const Relex&);
Relex& operator = (const Relex&);
public:
typedef T _HostType;
typedef T& _HostReference;
typedef T* _HostPointer;
public:
Relex() { }
T* GetHostPtr() { return reinterpret_cast(this); }
};

原理:
模板的迭代编译

使用:
template
struct A : Relex
{
void func1() { GetHostPtr()->func2(); }
};

template
struct B : Relex
{
void func2() { GetHostPtr()->func1(); }
};

templateclass T1, templateclass T2>
class C_Impl : public T1, public public T2 { };

typedef C_Impl C;



5。代码容器
这里的实现非常巨大,就不写了,你可以自己去看代码

实现:
template
struct Typelist //请想想STL里的list,非常象。这里的Tail又是一个Typelist,这样形成一个链
{
typedef T Head;
typedef U Tail;
};
//Typelist的头是Typelist,我们把NullType作为链的尾。

struct NullType { };
struct EmptyType { };

//下面是操作Typelist的类

template struct Length;
template struct TypeAt;
template struct TypeAtNonStrict;
template struct IndexOf;
template struct Append;
template struct Erase;
template struct EraseAll;
template struct NoDuplicates;
template struct Replace;
template struct ReplaceAll;
template struct Reverse;
template struct MostDerived;
template struct DerivedToFront;

//下面就是代码容器,典型的模板递归和模板偏特化的运用,请仔细看,其实很简单

template class Unit>
class GenScatterHierarchy;

template class Unit>
class GenScatterHierarchy, Unit>
: public GenScatterHierarchy
, public GenScatterHierarchy
{
public:
typedef Typelist TList;
typedef GenScatterHierarchy LeftBase;
typedef GenScatterHierarchy RightBase;
template struct Rebind
{
typedef Unit Result;
};
};

template class Unit>
class GenScatterHierarchy : public Unit
{
typedef Unit LeftBase;
template struct Rebind
{
typedef Unit Result;
};
};

template