关闭

模板元编程 第二章练习(非答案)

标签: c++模板元编程
136人阅读 评论(0) 收藏 举报
分类:

2-0.编写一个一元元函数 add_const_ref<T>,如果T是一个引用类型,就返回T,否则返回T const&。编写一个程序来测试你的元函数。提示:可以使用boost::is_same来测试结果。

boost::add_const 对引用没有效果。。

#ifndef ITER_SWAP_H
#define ITER_SWAP_H

#include<iterator>
#include<iostream>
#include<boost\type_traits\add_const.hpp>
#include<boost\type_traits\add_reference.hpp>
#include<boost\type_traits\is_reference.hpp>


template<bool,class T>
struct doit{
	typename typedef T type;
};

template<class T>
struct doit<true,T>{
	typename typedef T type;
};


template<class T>
struct doit<false,T>{
	typename typedef boost::add_const<T>::type type1;
	typename typedef boost::add_reference<type1>::type type;
};

template<class T>
struct add_const_ref
{				  
	static const bool isR = boost::is_reference<T>::value;
	typename typedef doit<isR,T>::type type;
};


#endif

2-1. 编写一个三元元函数replace_type,它接收一个任意的复合类型c作为其第一个参数,

  并将c中出现的所有type x替换为y

#pragma once


#include<iterator>
#include<boost\type_traits\add_const.hpp>
#include<boost\type_traits\add_reference.hpp>
#include<boost\type_traits\is_reference.hpp>
#include<boost\type_traits\is_same.hpp>

template<class A,class B,class C>
struct replace_type;

template<class A,class B,class C,bool>
struct replace_type_imp
{
	typename typedef C type;
};

template<class A,class B,class C>
struct replace_type_imp<A,B,C,false>
{
	typename typedef A type;
};

template<class A,class B,class C,int n>
struct replace_type_imp<A[n],B,C,false>
{
	typename typedef replace_type<A,B,C>::type type[n];
};
template<class A,class B,class C>
struct replace_type_imp<A[],B,C,false>
{
	typename typedef replace_type<A,B,C>::type type[];
};
template<class A,class B,class C>
struct replace_type_imp<A&,B,C,false>
{
	typename typedef replace_type<A,B,C>::type& type;
};
template<class A,class B,class C>
struct replace_type_imp<A*,B,C,false>
{
	typename typedef replace_type<A,B,C>::type* type;
};
template<class A,class B,class C>
struct replace_type_imp<A(),B,C,false>
{
	typename typedef replace_type<A,B,C>::type type();
};
template<class A,class B,class C,class arg1>
struct replace_type_imp<A(arg1),B,C,false>
{
	typename typedef replace_type<A,B,C>::type type(typename replace_type<arg1,B,C>::type);
};


template<class A,class B,class C>
struct replace_type
{
	static const bool value=boost::is_same<A,B>::value;
	typename typedef replace_type_imp<A,B,C,value>::type type;
};

2-2

#pragma once
#include<boost\type_traits\is_base_and_derived.hpp>
#include<assert.h>


template<class base,class derived>
struct polymorphic_downcast_imp{
	const static bool value =0;
};


template<class base,class derived>
struct polymorphic_downcast_imp<base,derived&>
{
	const static bool value = boost::is_base_and_derived<base,derived>::value;
};
template<class base,class derived>
struct polymorphic_downcast_imp<base*,derived*>
{
	const static bool value = boost::is_base_and_derived<base,derived>::value;
};




template<class derived,class base>
derived polymorphic_downcast(base x){
	const static bool value = polymorphic_downcast_imp<base,derived>::value;
	assert(value);
	return static_cast<derived>(x);
}


2-3,少了一个const;

#pragma once
#include<string>
using std::string;


template<class T>
struct type_descriptor_imp
{
	static const string value;
};
template<class T>
const string type_descriptor_imp<T>::value="unknow";
template<>
const string type_descriptor_imp<int>::value="int";
template<>
const string type_descriptor_imp<short int>::value="short int";
template<>
const string type_descriptor_imp<char>::value="char";
template<>
const string type_descriptor_imp<long int>::value="long int";

template<class T>
struct type_descriptor
{
	operator const char* (){
		static string ret;
		ret = type_descriptor_imp<T>::value;
		return ret.c_str();
	}
};
template<class T>
struct type_descriptor<T&>
{
	operator const char* (){
		static string ret;
		ret = type_descriptor_imp<T>::value;
		ret+="&";
		return ret.c_str();
	}
};


template<class T>
struct type_descriptor<T[]>
{
	operator const char* (){
		string ret;
		ret = type_descriptor_imp<T>::value;
		ret+="[]\0";
		return ret.c_str();
	}
};

template<class T>
struct type_descriptor<T*>
{
	operator const char* (){
		static string ret;
		ret = type_descriptor_imp<T>::value;
		ret+="*";
		return ret.c_str();
	}
};



template<class T,class arg>
struct type_descriptor<T(*)(arg)>
{
	operator const char* (){
		static string ret;
		ret = type_descriptor<T>();
		ret+="(*)(";
		ret+=type_descriptor<arg>();
		ret+=")";
		return ret.c_str();
	}
};
template<class T>
struct type_descriptor<T(*)()>
{
	operator const char* (){
		static string ret;
		ret = type_descriptor<T>();
		ret+="(*)()";
		return ret.c_str();
	}
};





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11015次
    • 积分:843
    • 等级:
    • 排名:千里之外
    • 原创:76篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条