単一のタイプint
とタイプのリストの組み合わせのリストは、次のmpl::vector<int, long>
呼び出しによって計算できますmpl::fold
。
typedef fold<
mpl::vector<int, long>, vector<>,
push_back<mpl::_1, std::pair<int, mpl::_2> >
>::type list_of_pairs;
ここで、それを別のメタ関数にラップし、初期型リストのすべての型に対して呼び出すと、次のようになります。
typedef mpl::vector<int, long> typelist;
template <typename T, typename Result>
struct list_of_pairs
: mpl::fold<typelist, Result,
mpl::push_back<mpl::_1, std::pair<T, mpl::_2> > >
{};
typedef mpl::fold<
typelist, mpl::vector<>, mpl::lambda<list_of_pairs<mpl::_2, mpl::_1> >
>::type result_type;
BOOST_MPL_ASSERT(
mpl::equal<result_type,
mpl::vector4<
std::pair<int, int>, std::pair<int,long>,
std::pair<long,int>, std::pair<long,long>
> >::value);
編集:2番目の質問に答える:
(あなたが言及した意味で)一意の要素のみを含む結果を作成することは、もう少し複雑です。最初に、2 つの要素を比較して mpl::true_/mpl::false_ を返すメタ関数を定義する必要があります。
template <typename P1, typename P2>
struct pairs_are_equal
: mpl::or_<
mpl::and_<
is_same<typename P1::first_type, typename P2::first_type>,
is_same<typename P1::second_type, typename P2::second_type> >,
mpl::and_<
is_same<typename P1::first_type, typename P2::second_type>,
is_same<typename P1::second_type, typename P2::first_type> > >
{};
次に、特定のリストで特定の要素を見つけようとするメタ関数を定義する必要があります。
template <typename List, typename T>
struct list_doesnt_have_element
: is_same<
typename mpl::find_if<List, pairs_are_equal<mpl::_1, T> >::type,
typename mpl::end<List>::type>
{};
これを利用して新しいリストを作成し、重複が挿入されないようにすることができます。
typedef mpl::fold<
result_type, mpl::vector<>,
mpl::if_<
mpl::lambda<list_doesnt_have_element<mpl::_1, mpl::_2> >,
mpl::push_back<mpl::_1, mpl::_2>, mpl::_1>
>::type unique_result_type;
これはすべて私の頭の上からのものであるため、あちこちで微調整が必要になる場合があります。しかし、その考えは正しいはずです。
EDIT:@rafakによって概説されたマイナーな修正