Chinaunix首页 | 论坛 | 博客
  • 博客访问: 145890
  • 博文数量: 124
  • 博客积分: 70
  • 博客等级: 民兵
  • 技术积分: 1745
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 13:49
文章分类

全部博文(124)

文章存档

2011年(55)

2010年(14)

2009年(30)

2008年(25)

我的朋友

分类: WINDOWS

2011-01-25 17:11:53

// 2.1 题目
#include
#include
#include

// 前置声明
template
class replace_compoundtype;

template
class replace_simpletype;

template
class replace_type;

// 基本替换类型,所有无法匹配的默认版本
template
class replace_type_
{
public:
static const bool bSame = boost::is_same::value;

// 选择一下
typedef typename boost::mpl::if_c
V,
T>::type type;
};

// T is a simple type
template
class replace_type_
{
public:
static const bool bConst = boost::is_const::value;

// 选择一下
typedef typename boost::mpl::if_c
typedef typename replace_type_::type,V>::type,
typedef typename boost::add_const::type>::type
>::type type;

};

template
class replace_type_
{
public:
static const bool bVolatile = boost::is_volatile::value;
// 选择一下
typedef typename boost::mpl::if_c
typedef typename replace_type_::type,V>::type,
typedef typename boost::add_volatile::type>::type
>::type type;
};


template
class replace_type_
{
public:
static const bool bVolatile = boost::is_volatile::value;

// 选择一下
typedef typename boost::mpl::if_c
typedef typename replace_type_::type,V>::type,
typedef typename boost::add_volatile<
typedef typename replace_type_::type
>::type 
>::type type;
};


template
class replace_type_
{
public:
static const bool bCompound = boost::is_compound::value;

// 选择一下
typedef typename boost::mpl::if_c
typename replace_compoundtype::type,
typename replace_simpletype::type>::type type_;

typedef typename boost::add_pointer::type type;
};

template
class replace_type_
{
public:
static const bool bCompound = boost::is_compound::value;

// 选择一下
typedef typename boost::mpl::if_c
typename replace_compoundtype::type,
typename replace_simpletype::type >::type type;
};

template
class replace_type_
{
public:
static const bool bCompound = boost::is_compound::value;

// 选择一下
typedef typename boost::mpl::if_c
typename replace_compoundtype::type,
typename replace_simpletype::type >::type type_;

typedef typename boost::add_reference::type type;
};

template
class replace_type_
{
public:
static const bool bCompound = boost::is_compound::value;

// 选择一下
typedef typename boost::mpl::if_c
typename replace_compoundtype::type,
typename replace_simpletype::type >::type type;
};


// T is a simple type
template
class replace_simpletype
{
public:
typedef typename replace_type_::type type;
};

template
class replace_function
{
public:
typedef T type;
};


// 这儿简单些,我支持0 or 1个参数
template
class replace_function
{
public:
typedef typename replace_type::type R_;
typedef typename replace_type::type P1_;
typedef R_ (*PTR)(P1_) ;
typedef PTR type;
};

template
class replace_function
{
public:
typedef typename replace_type_::type R_;
typedef R_ (*PTR)();
typedef PTR type;
};


//
template
class replace_type_
{
public:
typedef typename replace_function::type type;
};

template
class replace_type_
{
public:
typedef typename replace_function::type type;
};



// T  is a compound type
template
class replace_compoundtype
{
public:
// 区分引用 ,数组,指针,函数指针 4种类型; 成员函数指针除外
static const bool bArray = boost::is_array::value;
static const bool bRef = boost::is_reference::value;
static const bool bPointer = boost::is_pointer::value;
static const bool bFuncPointer = boost::is_function::value;

typedef typename boost::mpl::if_c
typename typename replace_function::type,
typename typename replace_type_::type>::type typeFunctionPtrOrDefault;

typedef typename boost::mpl::if_c
typename replace_type_::type,
typeFunctionPtrOrDefault>::type typePtrOrNot;

typedef typename boost::mpl::if_c
typename replace_type_::type,
typePtrOrNot>::type typeRefOrNot;

typedef typename boost::mpl::if_c
typename replace_type_::type ,U,V>::type,
typeRefOrNot>::type typeArrayOrNot;

typedef typename boost::mpl::if_c
typeFunctionPtrOrDefault,
typedef typename boost::mpl::if_c
typeRefOrNot,
typedef typename boost::mpl::if_c
typePtrOrNot,
typedef typename boost::mpl::if_c
typeFunctionPtrOrDefault,
T>::type
>::type
>::type
>::type type;
};




// 这个问题,我理解为处理基本类型,处理数组,处理函数指针,且函数指针携带的类型是基本类型cv
// 其中基本类型也就 is_compound 表示未false_type的那类cv or ;else 就是复合类型
// 另外U V是cv 的基本类型,或者& *

template
class replace_type
{
public:
static const bool bCompound = boost::is_compound::value;

// 选择一下
typedef typename boost::mpl::if_c
typename replace_compoundtype::type,
typename replace_simpletype::type >::type type;
};




int _tmain(int argc, _TCHAR* argv[])
{
// 2.1 练习题

// simple类型测试 

{

// 对ref添加 add_volatile / add_const 那么将保持原类型输出


float f = (float)0.1;
boost::add_volatile::type i = f;
boost::add_const::type i2 = f;

replace_type::type f1 = 0; // float
replace_type::type f2 = 0; // const float
replace_type::type f3 = 0; // volatile float
replace_type::type f4 = 0; // const volatile float
replace_type::type f5 = f; // volatile float& == float&
replace_type::type f6 = 0; // const float
replace_type::type f7 = 0; // const float
}

{
float f = (float)0.1;
volatile float & ff = f;
replace_type::type f1 = 0; // float*
replace_type::type f2 = 0; // const float*
replace_type::type f3 = 0; // volatile float*
replace_type::type f4 = 0; // const volatile float*
replace_type::type f5 = f; // volatile float& = float&
replace_type::type f6 = f;// const float&
replace_type::type f7 = 0; // const float**

f=1.0;
}

{
float f = (float)0.1;
replace_type::type f1 = f; // float&
replace_type::type f2 = 0; // const float&
replace_type::type f3 = f; // volatile float&
replace_type::type f4 = 0; // const volatile float&
replace_type::type f5 = 0; // volatile float*
replace_type::type f6 = 0;//const float*
replace_type::type f7 = f; // const float&

f=1.0;
}

// 函数替换
{
float f = 0.;
replace_type::type f1; // float& (*)()
replace_type::type f2; // float& (*)(float)

replace_type::type f3; // float& (*)(float)
replace_type::type f4; // float& (*)(cv float&)
f = 1.0;

}

return 0;
}

阅读(456) | 评论(1) | 转发(0) |
0

上一篇:leak dlg

下一篇:符号服务器与source svr

给主人留下些什么吧!~~

chinaunix网友2011-03-07 08:46:33

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com