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,返回值>代替