0

我有一个元素boost::mpl::vectorN说:

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

我希望获得一个M包含my_vector. 所以如果M是 2 我想要一个:

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_elements采用 MPL 积分常数和first_n_elements_c只采用整数的 a。

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 回答