0

私は要素を持っています、例えばboost::mpl::vectorN

typedef boost::mpl::vector<int,float,double,short,char> my_vector;

Mの最初の要素を含むシーケンスを取得したいと思いmy_vectorます。したがって、M2の場合、次のようにします。

typedef boost::mpl::vector<int,float> my_mvector;

最初は使用することを考えましたが、とのerase<s,first,last>適切なテンプレートパラメータを見つけることができませんでした。(使用していました。)しかし、タスクにも使用できるのも私の理解です。これを行うための最良の方法は何ですか?firstlastat_c<...>::typefilter_view

4

1 に答える 1

3

消去は、問題に対する合理的な解決策です。

  • 最初に必要な値はmpl::begin<T>、返すことに関心のある要素の数だけ進んだ結果です。
  • endに必要な値は、次の結果です。mpl::end<T>

以下のコードは、ベクトル内の要素の数が要求された数より少ない場合に、メタ関数が元の型を返すようにすることを前提としています。静的アサーションを使用して、入力積分タイプがベクトルのサイズ以下であることを確認することもできます。

first_n_elementsMPL積分定数をとるaと単純に整数をとるaの両方を提供しましたfirst_n_elements_c

iterator_range<>ビューを使用する場合は、以下のコードのbeginおよびcutイテレータと一緒に使用することもできます。この場合、一方が他方に対してどのような利点があるのか​​わかりません。

#include <boost/mpl/vector.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/erase.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/less.hpp>
namespace mpl = boost::mpl;



namespace detail 
{
  // Note, this is an internal detail.  Please use the structures below
  template <typename T, typename N>
  struct erase_after_n
  {
    typedef typename mpl::begin<T>::type begin_iter;
    typedef typename mpl::advance<begin_iter, N>::type cut_iter;
    typedef typename mpl::end<T>::type end_iter;

    typedef 
    typename mpl::erase< T,cut_iter, end_iter >::type type;

  };

}


template <typename T, typename N> 
struct first_n_elements
{
  typedef 
  typename mpl::eval_if< mpl::less < mpl::size<T>, N >,
             T,
             detail::erase_after_n<T, N> >::type type;

};

template <typename T, int N> 
struct first_n_elements_c
{
  typedef 
  typename first_n_elements<T, mpl::int_<N> >::type type ;

};
于 2011-11-15T00:44:27.533 に答える