找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4283|回复: 1

C++模版元编程3.1节示例

[复制链接]
发表于 2008-9-20 17:36:32 | 显示全部楼层 |阅读模式
3.1节是以一个"量纲分析"的程序作为示例来讲解的,我整理了一下,把代码发上来了
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/int.hpp>
#include <boost/static_assert.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/transform.hpp>
#include<boost/mpl/placeholders.hpp>
namespace mpl = boost::mpl;
using namespace mpl::placeholders;
template <class T, class Dimensions>
    struct quantity
    {
        explicit quantity(T x)
           : m_value(x)
        {}
  // converting constructor
        template <class OtherDimensions>
        quantity(quantity<T,OtherDimensions> const& rhs)
          : m_value(rhs.value())
        {
        }

        T value() const { return m_value; }
     private:
        T m_value;
    };
template <class T, class D1, class D2>
    quantity<
        T
      , typename mpl::transform<D1,D2,mpl::minus<_1,_2> >::type
    >
    operator/(quantity<T,D1> x, quantity<T,D2> y)
    {
       typedef typename
         mpl::transform<D1,D2,mpl::minus<_1,_2> >::type dim;
       return quantity<T,dim>( x.value() / y.value() );
    }
template <class T, class D1, class D2>
    quantity<
        T
      , typename mpl::transform<D1,D2,mpl::plus<_1,_2> >::type
    >
    operator*(quantity<T,D1> x, quantity<T,D2> y)
    {
       typedef typename
         mpl::transform<D1,D2,mpl::plus<_1,_2> >::type dim;
       return quantity<T,dim>( x.value() * y.value() );
    }

template <class T,class D>
    quantity<T,D> operator+(quantity<T,D> x,quantity<T,D> y)
    {
     return quantity<T,D>(x.value()-y.value());
    }
template <class T,class D>
   quantity<T,D> operator-(quantity<T,D> x,quantity<T,D> y)
   {
    return quantity<T,D>(x.value()-y.value());
   }

int _tmain(int argc, _TCHAR* argv[])
{
    typedef mpl::vector_c<int,1,0,0,0,0,0,0> mass;
    typedef mpl::vector_c<int,0,1,0,0,0,0,0> length;  // or position
    typedef mpl::vector_c<int,0,0,1,0,0,0,0> time;
    typedef mpl::vector_c<int,0,0,0,1,0,0,0> charge;
    typedef mpl::vector_c<int,0,0,0,0,1,0,0> temperature;
    typedef mpl::vector_c<int,0,0,0,0,0,1,0> intensity;
    typedef mpl::vector_c<int,0,0,0,0,0,0,1> angle;
typedef mpl::vector_c<int,0,1,-1,0,0,0,0> velocity;     // l/t
    typedef mpl::vector_c<int,0,1,-2,0,0,0,0> acceleration; // l/(t2)
    typedef mpl::vector_c<int,1,1,-1,0,0,0,0> momentum;     // ml/t
    typedef mpl::vector_c<int,1,1,-2,0,0,0,0> force;        // ml/(t2)
quantity<float,force> f(2.0f);
quantity<float,acceleration> a(1.0f);
quantity<float,mass> m(2.0f);
quantity<float,mass> m2=f/a;
std::cout<<typeid(m2).name()<<"\n";
std::cout<<typeid(m).name()<<"\n";
return 0;
}
 楼主| 发表于 2008-9-20 17:36:40 | 显示全部楼层
这一节的最后,作者还给出了量纲的运算,我认为,用量纲的运算结果来表示非基础量纲会更加清楚。如下:

template <class D1, class D2>
struct multiple_dimensions : mpl::transform<D1,D2,mpl::plus<_1,_2> >
{};

template <class D1, class D2>
struct divide_dimensions : mpl::transform<D1,D2,mpl::minus<_1,_2> >
{};

typedef divide_dimensions<length, time>::type velocity;
typedef divide_dimensions<velocity, time>::type acceleration;
typedef multiple_dimensions<mass, velocity>::type momentum;
typedef multiple_dimensions<mass, acceleration>::type force;
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-12-22 18:27 , Processed in 0.022707 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表