消去は、問題に対する合理的な解決策です。
- 最初に必要な値は
mpl::begin<T>
、返すことに関心のある要素の数だけ進んだ結果です。
- endに必要な値は、次の結果です。
mpl::end<T>
以下のコードは、ベクトル内の要素の数が要求された数より少ない場合に、メタ関数が元の型を返すようにすることを前提としています。静的アサーションを使用して、入力積分タイプがベクトルのサイズ以下であることを確認することもできます。
first_n_elements
MPL積分定数をとる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 ;
};