添加我玩 boost::mpl::* 的那一刻,并尝试扩展物理单元示例。目前我有以下代码:
由 mpl::vector 表示的简单物理基本维度:
template < int Mass, int Length, int Time, int Temperature, int Angle, int Current >
struct base_dimension
{
typedef typename mpl::vector_c< int, Mass, Length, Time, Temperature, Angle, Current >::type type;
};
一个标量向量:
typedef base_dimension< 0, 0, 0, 0, 0, 0 >::type base_dimensionless_helper;
存储 base_dimension 类型的简单类型:
template< class base_dimension >
struct Dimension
{
typedef Dimension< base_dimension > type;
typedef base_dimension base_dim_type;
};
一个标量维度:
typedef Dimension< base_dimensionless_helper > base_dimensionless;
一些简单的帮手:
template < class D1, int fac >
struct mul_base_dim_fac_typeof
{
typedef typename base_dimension< fac, fac, fac, fac, fac, fac >::type fac_vec;
typedef typename Detail::multiply_typeof_helper< typename D1, fac_vec >::type type;
};
template < class Dim1, class Dim2 >
struct add_dim_typeof_helper
{
typedef typename add_base_dim_typeof_helper< typename Dim1::base_dim_type, typename Dim2::base_dim_type >::type dim;
typedef Dimension< dim > type;
};
最后是派生维度。使用的公式是:(D0 * E0) + (D1 * E1) +...+ (D5 * E5) ... ::type
template < class D0 = base_dimensionless, int E0 = 0,
class D1 = base_dimensionless, int E1 = 0,
class D2 = base_dimensionless, int E2 = 0,
class D3 = base_dimensionless, int E3 = 0,
class D4 = base_dimensionless, int E4 = 0,
class D5 = base_dimensionless, int E5 = 0 >
struct derived_dimension
{
typedef Dimension< typename mul_base_dim_fac_typeof< typename D0::base_dim_type, E0 >::type > d0_type;
typedef Dimension< typename mul_base_dim_fac_typeof< typename D1::base_dim_type, E1 >::type > d1_type;
typedef Dimension< typename mul_base_dim_fac_typeof< typename D2::base_dim_type, E2 >::type > d2_type;
typedef Dimension< typename mul_base_dim_fac_typeof< typename D3::base_dim_type, E3 >::type > d3_type;
typedef Dimension< typename mul_base_dim_fac_typeof< typename D4::base_dim_type, E4 >::type > d4_type;
typedef Dimension< typename mul_base_dim_fac_typeof< typename D5::base_dim_type, E5 >::type > d5_type;
typedef typename Detail::add_dim_typeof_helper< d0_type, d1_type >::type d0_d1_type;
typedef typename Detail::add_dim_typeof_helper< d0_d1_type, d2_type >::type d0_d1_d2_type;
typedef typename Detail::add_dim_typeof_helper< d0_d1_d2_type, d3_type >::type d0_d1_d2_d3_type;
typedef typename Detail::add_dim_typeof_helper< d0_d1_d2_d3_type, d4_type >::type d0_d1_d2_d3_d4_type;
typedef typename Detail::add_dim_typeof_helper< d0_d1_d2_d3_d4_type, d5_type >::type type;
};
好的,这确实按预期工作。但是我想美化派生维度的计算,因为所有这些中间类型定义都很丑陋。我的第一个想法是将输入向量推入一个向量(-> 一个向量向量),然后在一个不错的 for_each 循环中将它们相乘,但直到现在都没有成功。所以我的问题是:
任何提示如何美化计算?