Boost.Hana  1.6.0
Your standard library for metaprogramming
boost::mpl::list< T > Struct Template Reference

Description

template<typename ... T>
struct boost::mpl::list< T >

Adapter for Boost.MPL lists.

Modeled concepts

It is possible for MPL lists to model a couple of concepts. However, because they are only able to hold types, they lack the generality required to model concepts like Functor, Sequence and other related concepts.

  1. Comparable
    Two MPL lists are equal if and only if they contain the same number of types, and if all those types are equal.
    // Copyright Louis Dionne 2013-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    namespace hana = boost::hana;
    namespace mpl = boost::mpl;
    hana::equal(mpl::list2<int, char>{}, mpl::list<int, char>{})
    );
    hana::not_equal(mpl::list2<int, char>{}, mpl::list<int, char, float>{})
    );
    int main() { }
  2. Foldable
    Folding a MPL list is equivalent to folding it as a Sequence.
    // Copyright Louis Dionne 2013-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    #include <type_traits>
    namespace hana = boost::hana;
    namespace mpl = boost::mpl;
    auto types = mpl::list<long, float, short, float, long, long double>{};
    auto number_of_floats = hana::fold_left(types, hana::int_c<0>, [](auto count, auto t) {
    return hana::if_(hana::trait<std::is_floating_point>(t),
    count + hana::int_c<1>,
    );
    });
    BOOST_HANA_CONSTANT_CHECK(number_of_floats == hana::int_c<3>);
    int main() { }
  3. Iterable
    Iterating over a MPL list is just iterating over each of the types it contains, as if it were a Sequence.
    // Copyright Louis Dionne 2013-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    #include <type_traits>
    namespace hana = boost::hana;
    namespace mpl = boost::mpl;
    BOOST_HANA_CONSTANT_CHECK(hana::front(mpl::list<int, char, void>{}) == hana::type_c<int>);
    hana::drop_front(mpl::list<int, char, void>{}),
    mpl::list<char, void>{}
    ));
    hana::drop_while(mpl::list<float, double const, int, float&>{},
    hana::trait<std::is_floating_point>),
    mpl::list<int, float&>{}
    ));
    int main() { }
  4. Searchable
    A MPL list can be searched as if it were a tuple containing hana::types.
    // Copyright Louis Dionne 2013-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    namespace hana = boost::hana;
    namespace mpl = boost::mpl;
    hana::find_if(mpl::list<int, float, char const*>{}, hana::equal.to(hana::type_c<float>))
    ==
    hana::just(hana::type_c<float>)
    );
    hana::find(mpl::list<int, float, char const*>{}, hana::type_c<void>)
    ==
    hana::nothing
    );
    int main() { }

Conversion from any Foldable

A MPL list can be created from any Foldable. More precisely, for a Foldable xs whose linearization is [x1, ..., xn],

to<ext::boost::mpl::list_tag>(xs) == mpl::list<t1, ..., tn>{}

where tk is the type of xk, or the type contained in xk if xk is a hana::type.

Warning
The limitations on the size of mpl::lists are inherited by this conversion utility, and hence trying to convert a Foldable containing more than BOOST_MPL_LIMIT_LIST_SIZE elements is an error.
// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
#include <type_traits>
namespace hana = boost::hana;
namespace mpl = boost::mpl;
auto xs = hana::make_tuple(hana::type_c<int>, hana::type_c<char>, hana::type_c<double>);
static_assert(std::is_same<
decltype(hana::to<hana::ext::boost::mpl::list_tag>(xs)),
mpl::list<int, char, double>
>{}, "");
int main() { }