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

本文探讨了模板元编程的第六章相关习题,主要涉及C++中的模板元编程技术及其应用。
摘要由CSDN通过智能技术生成
//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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值