4

次のコードがあります。

#include <boost/mpl/list_c.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/assert.hpp>
#include <iostream>
#include <typeinfo>
#include <assert.h>

using namespace boost::mpl;

typedef list_c<long,0,2,4,6,8,10> evens;
typedef list_c<long,2,3,5,7,11,13> primes;
typedef list_c<long,2,5,9,13,19,23> sums;

typedef transform< evens, primes, plus<> >::type result;
BOOST_MPL_ASSERT(( equal< result,sums,equal_to<_1,_2> > ));

int main()
{
    std::cout << typeid(sums).name() << std::endl << typeid(result).name() << std::endl;
    assert(typeid(sums) == typeid(result));
}

コンパイルされるので、BOOST_MPL_ASSERT が保持されます。ただし、実行すると、メイン関数のアサーションが失敗します。どういう意味ですか?同じ要素を含む 2 つの list_c のもの (適切な単語がないようです) が同じ型を定義するべきではありませんか?

ご協力ありがとうございました。

4

2 に答える 2

1

transformintegral_c<long, n>ではなく、指定されていない (要素の)シーケンス タイプを出力しますlist_c

の結果をtransform既知の型に変換する 1 つの方法は、次のように使用insert_rangeすることlistです。

insert_range<list0<>, begin<list0<> >::type, result>::type

残念ながら、これはlist内部mpl::l_item型ではなく を生成しないため、両側で同じ操作を行う必要があります。

BOOST_MPL_ASSERT((is_same<
      insert_range<list0<>, begin<list0<> >::type, result>::type,
      insert_range<list0<>, begin<list0<> >::type, sums>::type>));

reverse_copy同様に、フロントインサーターに使用できます。

BOOST_MPL_ASSERT((is_same<
      reverse_copy<result, front_inserter<list0<> > >::type,
      reverse_copy<sums, front_inserter<list0<> > >::type
      >));

実際、front_inserterは組み込みのメタ関数 の観点から実装されているためpush_front、これは と同じ型を生成しinsert_rangeます。

copyただし、常に同じ未指定のシーケンス型を生成することに依存する方が簡単です。

BOOST_MPL_ASSERT((is_same<copy<result>::type, copy<sums>::type>));

は変換アルゴリズムであるためcopy、 と同じ型を返すtransformため、次のように記述できます。

BOOST_MPL_ASSERT((is_same<result, copy<sums>::type>));

assert(typeid(copy<sums>::type) == typeid(result));
于 2012-11-29T18:24:54.920 に答える
1

MPL は、MPL アルゴリズムから得られる正確な型について保証しません。

于 2012-11-29T18:13:04.967 に答える