您的位置:首页 > 编程语言

模板元编程第六章课后习题(非答案)20151206

2015-12-05 16:25 495 查看
//6-0
template<bool,class T1,class T2>
struct T1smallest{
typedef T1 type;
};
template<class T1,class T2>
struct T1smallest<false,T1,T2>{
typedef T2 type;
};

template<class T1,class T2>
struct T1smallest_imp:T1smallest<(mpl::sizeof_<T1>::type::value<mpl::sizeof_<T2>::type::value),T1,T2>{};

template<class Seq>
struct smallest:mpl::copy<Seq,mpl::inserter<long long,T1smallest_imp<_1,_2> >>{};
//6-0


//6-3

//toy_tree.h
#pragma once
#include<boost\mpl\vector.hpp>
#include<boost\mpl\placeholders.hpp>
using namespace boost;
using namespace mpl::placeholders;
//5-10
struct nothing{};
template<typename T0=nothing,typename T1=nothing,typename T2=nothing>
struct tree
{
typedef tree type;
typedef T0 root;
typedef T1 lson;
typedef T2 rson;
};

template<typename vec,typename T>//T是个普通类型
struct inorder_view_imp:mpl::push_back<vec,T>{};
template<typename vec>
struct inorder_view_imp<vec,nothing>{//nothing
typedef vec type;
};

template<typename vec,typename T0,typename T1,typename T2>//T是树
struct inorder_view_imp<vec,tree<T0,T1,T2> >
{

typedef typename mpl::push_back<typename inorder_view_imp<vec,T1>::type,T0>::type tmpvec;
typedef typename inorder_view_imp<tmpvec,T2>::type type;
};

template<class T>
struct inorder_view:inorder_view_imp<mpl::vector<>,T>::type{};

//5-10
template<class T,class K>
struct smaller{
typedef mpl::bool_<T::value<K::value> type;
};

template<bool,class Tree,class T>
struct push_back_tree;
template<bool,class Tree,class T>
struct push_back_type{
typedef tree<Tree,T> type;
};
template<class Tree,class T>
struct push_back_type<true,Tree,T>{
typedef tree<Tree,nothing,T> type;
};

template<class Tree,class T>
struct push_back//对一个类型push_back
:push_back_type<smaller<Tree,T>::type::value,Tree,T>{};

template<class T0,class T1,class T2,class T>
struct push_back<tree<T0,T1,T2>,T>//对树push_back
:push_back_tree<smaller<T0,T>::type::value,tree<T0,T1,T2>,T >{};

template<class T>
struct push_back<nothing ,T>
{
typedef T type;
};
template<class T>
struct push_back<tree<>,T>:tree<T>{};

template<class Tree,class T>
struct push_back_tree<false,Tree,T>{
typedef tree<typename Tree::root,typename push_back<typename Tree::lson,T>::type,typename Tree::rson> type;
};
template<class Tree,class T>
struct push_back_tree<true,Tree,T>{
typedef tree<typename Tree::root,typename Tree::lson,typename push_back<typename Tree::rson,T>::type> type;
};

template<class tree>
struct binary_tree_inserter
{
typedef tree state;
typedef push_back<_,_> operation;
};


//6-3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: