У меня есть список типов, определенных как:
typedef boost::mpl::list<Apple, Pear, Brick> OriginalList;
Я хотел бы создать второй список, который не содержит никаких фруктов, т.е. результирующий список, сформированный из первого списка, будет содержать один типКирпич.Fruit идентифицируется с помощью статической переменной 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;
}