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

9-0

#ifdef NO_MI_EBO
template<class F,bool,class G,bool>class storage;
template<class T>
class storage_imp:private T{
protected:
public:
	storage_imp(T const& t):T(t){}
	T const& get_t(){return *this;}
};
template<class F,class G>
class storage<F,true,G,true>:public storage_imp<F>,G{
	typedef storage_imp<F> base;
protected:
	storage(F const& f,G const& g):base(f),G(g){}
	F const& get_f(){return this->get_t();}
	G const& get_g(){return *this;}
};
#else
//原版本
#endif


9-1

其他部分与上面一致

template<class F,class G>
class storage<F,true,G,true>:public storage_imp<F>,G{
	BOOST_STATIC_ASSERT((boost::is_same<F,G>::value!=1));
	typedef storage_imp<F> base;
protected:
	storage(F const& f,G const& g):base(f),G(g){}
	F const& get_f(){return this->get_t();}
	G const& get_g(){return *this;}
};
template<class F>
class storage<F,true,F,true>:F{
protected:
	storage(F const& f,F const&):F(f){}
	F const& get_f(){return *this;}
	F const& get_g(){return *this;}
};

9-4

#pragma once

#include<boost\type_traits\is_arithmetic.hpp>
template<bool,class F>
struct enable_if{
	typedef F type;
};

template<class F>
struct enable_if<false,F>{};

template<bool,class F>
struct disable_if{
	typedef F type;
};
template<class F>
struct disable_if<true,F>{};


template<class T>
struct iterator_type
{
	typedef typename T::value_type type;
};

template<class T,bool>
struct deepest_type_imp
{
	typedef typename iterator_type<typename T::iterator>::type Ttype;
	typedef typename deepest_type_imp<Ttype,boost::is_arithmetic<Ttype>::value>::type type;
};
template<class T>
struct deepest_type_imp<T,true>{typedef T type;};


template<class T>
struct deepest_type:deepest_type_imp<typename iterator_type<T>::type,boost::is_arithmetic<typename iterator_type<T>::type>::value>{};

template<class iterator>
typename disable_if<boost::is_arithmetic<typename iterator_type<iterator>::type>::value,
		   typename deepest_type<iterator>::type
		  >::type
			 sum(iterator start,iterator end)
{
	typename deepest_type<iterator>::type x(0);
	for(;start!=end;++start){
		x+=sum(start->begin(),start->end());
	}
	return x;
};

template<class iterator>
typename enable_if<boost::is_arithmetic<typename iterator_type<iterator>::type>::value,
					typename iterator_type<iterator>::type>::type 
					sum(iterator start,iterator end)
{
	typename iterator_type<iterator>::type x(0);
	for(;start!=end;++start){
		x+= *start;
	}
	return x;
};


9-5

原来用BOOST_STATIC_ASSERT((mpl::equal<dimensions,otherdimensions>::type::value));  侦测量纲是否为同一序列,是则执行转换,

用enable_if<mpl::equal<dimensions,otherdimensions>::type::value,返回值>代替

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值