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

模板元编程 第三章课后练习(非答案)

2015-11-26 12:45 495 查看
3-0

template<int N>
struct cal
{
BOOST_STATIC_ASSERT((N%10>=0));
BOOST_STATIC_ASSERT((N%10<2));
static const int val = 2*cal<N/10>::val+N%10;
};

template<>
struct cal<0>
{
static const int val = 0;
};


3-1,3-2

BOOST_STATIC_ASSERT((mpl::equal<vector_c<int,2,3,4>,mpl::transform<vector_c<int,1,2,3>,vector_c<int,1,1,1>,plus_f>::type>::type::value));
BOOST_STATIC_ASSERT((mpl::equal<vector_c<int,1,4,9>,mpl::transform<vector_c<int,1,2,3>,vector_c<int,0,2,6>,plus_f>::type>::type::value));


3-3,3-4其实差不多

template<typename F,typename T>
struct fourtimes:twice<F,typename twice<F,T>::type>{};


3-5

template<typename T,typename D1,typename D2>
quantity<T,D1> operator+(quantity<T,D1> a,quantity<T,D2> b){
return operator+(a,quantity<T,D1>(b));
}


相关代码

#include<iostream>
#include<vector>
#include<map>
#include<boost\type_traits\is_same.hpp>
#include"test_staticAccert.h"
#include<boost\mpl\lambda.hpp>
#include<boost\mpl\apply.hpp>
#include<boost\mpl\plus.hpp>
#include<boost\mpl\placeholders.hpp>
#include<boost\mpl\vector_c.hpp>
#include<boost\mpl\transform.hpp>
#include<boost\mpl\equal.hpp>
#include<boost\type_traits\add_pointer.hpp>
#include<boost\mpl\apply.hpp>
using namespace std;
using namespace boost;
using namespace mpl::placeholders;
using mpl::vector_c;
typedef mpl::vector_c<int,0,1,-1,0,0,0,0> velocity;
typedef mpl::vector_c<int,0,1,-2,0,0,0,0> acceleration;
typedef mpl::vector_c<int,1,0,0,0,0,0,0> momentum;
typedef mpl::vector_c<int,1,1,-2,0,0,0,0> force;
typedef mpl::vector_c<int,0,1,-1,0,0,0,0> scalar;

typedef mpl::apply<_1,mpl::plus<_1,_2> >::type t2;

struct plus_f{
template<typename T,typename D>
struct apply{
typedef typename mpl::plus<T,D>::type type;
};
};
struct minus_f{
template<typename T,typename D>
struct apply:mpl::minus<T,D>{};
};

template<typename T,typename dimensions>
struct quantity
{
explicit quantity(T x):m_value(x){}
T value() const {return m_value;}
template<typename otherdimensions>
quantity(quantity<T,otherdimensions> const &x):m_value(x.value()){
BOOST_STATIC_ASSERT((mpl::equal<dimensions,otherdimensions>::type::value));
}
private:
T m_value;
};

template<typename T,typename D>
quantity<T,D> operator+(quantity<T,D> a,quantity<T,D> b){
return quantity&l
4000
t;T,D>(a.value()+b.value());
}
template<typename T,typename D1,typename D2> quantity<T,D1> operator+(quantity<T,D1> a,quantity<T,D2> b){ return operator+(a,quantity<T,D1>(b)); }
template<typename T,typename D>
quantity<T,D> operator-(quantity<T,D> a,quantity<T,D> b){
return quantity<T,D>(a.value()-b.value());
}
template<typename T,typename D1,typename D2>
quantity<T,typename mpl::transform<D1,D2,plus_f>::type> operator*(quantity<T,D1> a,quantity<T,D2> b){
typedef typename mpl::transform<D1,D2,mpl::plus<_1,_2>>::type type;
return quantity<T,type>(a.value()*b.value());
}

template<typename F,typename T>
struct twice:F::template apply<typename F::template apply<T>::type>{//转发元函数

};

struct add_pointer_f
{

template<typename T>
struct apply:boost::add_pointer<T>
{

};
};

template<typename F,typename T> struct fourtimes:twice<F,typename twice<F,T>::type>{};

int main(){
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  模板元编程