![](/img/trans.png)
[英]How to transform boost::mpl::vector into another boost::mpl::vector?
[英]How to optimize boost::mpl::vector code?
加上我在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;
};
好的,這確實按預期工作。 但是我想美化派生維度的計算,因為所有這些中間typedef都很丑陋。 我的第一個想法是將輸入向量推入一個向量(->向量的向量),並在一個不錯的for_each循環中將它們相乘,但直到現在都沒有成功。 所以我的問題是:
有什么提示如何美化計算嗎?
您可以先定義一個輔助函數來闡明乘法:
template < typename D, int E >
struct multiply
{
typedef Dimension< typename
mul_base_dim_fac_typeof< typename
D::base_dim_type,
E
>::type
> type;
};
您還可以使用諸如transform
和fold
算法(嗯,您寧願需要reduce
,但MPL中未提供):
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 mpl::vector<
mpl::pair< D0, E0 >, mpl::pair< D1, E1 >,
mpl::pair< D2, E2 >, mpl::pair< D3, E3 >,
mpl::pair< D4, E4 >, mpl::pair< D5, E5 >
> pairs;
typedef typename
mpl::transform<
pairs,
multiply< mpl::first< mpl::_1 >, mpl::second< mpl::_1 >;
>::type products;
typedef typename
mpl::fold<
products,
base_dimensionless,
add_dim_typeof_helper< mpl::_, mpl::_ >
>::type type;
};
免責聲明:我根本沒有測試過此代碼,它的目的只是讓您了解使用MPL可以做什么。
或者,您可以使用預處理器來生成連續的typedef,可能需要借助Boost.Preprocessor 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.