基本量纲
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; //长度
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> amount_of_substance; // 量
复合量纲
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, 1, -1, 0, 0, 0, 0> momentum; //动量
typedef mpl::vector_c<int, 1, 1, -2, 0, 0, 0, 0> force; //力
类型定义
template <typename T, typename Dimensions>
class quantity
{
public:
explicit quantity(T x)
: m_value(x)
{}
template <typename OtherDimensions>
quantity(quantity<T, OtherDimensions> const& rhs)
: m_value(rhs.value())
{
static_assert(mpl::equal<Dimensions, OtherDimensions>::type::value, "type not equal");
}
T value() const { return m_value; }
private:
T m_value;
};
量纲的四则运算
// 加法
template <typename T, typename D1, typename D2>
quantity<T,D>
operator+(quantity<T, D1> x, quantity<T, D2> y)
{
static_assert(mpl::equal<D1, D2>::type::value, "type not equal");
return quantity<T, D1>(x.value() + y.value());
}
// 减法
template <typename T, typename D1,typename D2 >
quantity<T, D>
operator-(quantity<T, D1> x, quantity<T, D2> y)
{
static_assert(mpl::equal<D1, D2>::type::value, "type not equal");
return quantity<T, D1>(x.value() - y.value());
}
// 乘法
template <typename T, typename D1, typename D2>
quantity<T,typename mpl::transform<D1,D2,mpl::plus<mpl::_1, mpl::_2>>::type>
operator*(quantity<T, D1> x, quantity<T, D2> y)
{
typedef typename mpl::transform<D1, D2, mpl::plus<mpl::_1, mpl::_2>>::type dim;
return quantity<T, dim>(x.value() * y.value());
}
// 除法
template <typename T, typename D1, typename D2>
quantity<T, typename mpl::transform<D1, D2, mpl::minus<mpl::_1, mpl::_2>>::type>
operator/(quantity<T, D1> x, quantity<T, D2> y)
{
typedef typename mpl::transform<D1, D2, mpl::minus<mpl::_1, mpl::_2>>::type dim;
return quantity<T, dim>(x.value() / y.value());
}