English 中文(简体)
1. 吸收病媒中的第一个M元素
原标题:Getting the first M elements of an mpl vector

我有<代码>boost:mpl:vector with Ns,表示:

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>,但未能列出适当的模板参数,用于firstlast。 (I is using at_c<...>:type . 然而,我的理解是,<代码>filter_view也可用于这项任务。 实现这一目标的最佳途径是什么?

最佳回答

时代是解决你问题的合理办法。

  • The value you want for first is the result of mpl::begin<T> which is advanced by the number of elements you are interested in returning.
  • The value you want for end is the result of mpl::end<T>

下面的守则假定,如果病媒中的元素数量少于要求的数量,那么你就希望能恢复原类型。 也可以用静态的断言来核实投入的整体类型低于或等于病媒的规模。

我引申了一张<代码>第一_n_elements,该编码含有一种MPL的固定不变和first_n_elements_c,后者简单地采用了一种分类。

如果你想使用一种观点,你也可以使用<代码>iterator_range<>以及以下代码中的起始和切割器。 我不敢肯定本案的利弊。

#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 ;

};
问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签