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

这篇博客详细探讨了模板元编程的第三章课后练习,涵盖了3-0到3-5的部分,通过示例代码深入解析了相关概念和技术应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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<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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值