模板元编程 第三章课后练习(非答案)
2015-11-26 12:45
495 查看
3-0
3-1,3-2
3-3,3-4其实差不多
3-5
相关代码
#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;
}
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;
}
相关文章推荐
- 模板元编程 第二章练习(非答案)
- 模板
- C++11实现placeholder
- 算法习题12:求1+2+…+n 不能使用关键字
- std::bind源码剖析
- C++模板元编程Type_traits
- G++隐藏"bug"及应对:未特化的模板中的static_assert(false)的两种处理方式
- Effective C++第七章-模板和泛型编程之traits和TMP(模板元编程)
- (C++)使用模板在编译期计算阶乘
- 基于C++11模板元编程实现Scheme中的list及相关函数式编程接口
- C++模板编程
- 模板元编程第九章练习(非答案)
- 模板元编程第五章练习(非答案)
- 模板元编程第四章练习(非答案)
- struts表单数据获取方式
- Java日期处理
- spring+hibernate+c3p0整合链接池配置-连接无法释放问题(已经解决)
- PHP开发工具及配置过程
- 在Java中,如何跳出当前的多重嵌套循环?
- [Java] 简单文件数据库实现