次のように定義されたタイプのリストがあります。
typedef boost::mpl::list<Apple, Pear, Brick> OriginalList;
果物を含まない 2 番目のリストを作成したいと思います。つまり、最初のリストから形成された結果のリストには、単一タイプの Brick が含まれます。果物は、型内で定義された static const 変数によって識別されます。
struct Apple
{
static const bool IsFruit = true;
};
私は現在、メタ関数クラスを作成し、boost::mpl::remove_if
. boost::mpl::lambda を使用して別のRemoveFruit
構造体の必要性を取り除くことで、これをよりエレガントにすることができるはずです。これを行う方法に関する提案はありますか?
現在の完全なコード:
include <boost/static_assert.hpp>
#include <boost/mpl/list.hpp>
#include <boost/mpl/remove_if.hpp>
#include <boost/mpl/size.hpp>
#include <iostream>
struct Apple
{
static const bool IsFruit = true;
};
struct Pear
{
static const bool IsFruit = true;
};
struct Brick
{
static const bool IsFruit = false;
};
typedef boost::mpl::list<Apple, Pear, Brick> OriginalList;
BOOST_STATIC_ASSERT(boost::mpl::size<OriginalList>::type::value == 3);
// This is what I would like to get rid of:
struct RemoveFruit
{
template <typename T>
struct apply
{
typedef boost::mpl::bool_<T::IsFruit> type;
};
};
// Assuming I can embed some predicate directly in here?
typedef boost::mpl::remove_if<
OriginalList,
RemoveFruit
>::type NoFruitList;
BOOST_STATIC_ASSERT(boost::mpl::size<NoFruitList>::type::value == 1);
int main()
{
std::cout << "There are " << boost::mpl::size<OriginalList>::type::value << " items in the original list\n";
std::cout << "There are " << boost::mpl::size<NoFruitList>::type::value << " items in the no fruit list\n";
return 0;
}