Boost.Hana  1.7.1
你的元编程标准库
boost::mpl::list< T > 结构模板引用

描述

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

Boost.MPL 列表的适配器。

建模概念

MPL 列表可以建模一些概念。然而,由于它们只能持有类型,因此缺乏建模 FunctorSequence 和其他相关概念所需的通用性。

  1. Comparable (可比较)
    两个 MPL 列表当且仅当它们包含相同数量的类型,并且所有这些类型都相等时才相等。
    // 版权 Louis Dionne 2013-2017
    // 根据 Boost 软件许可证第 1.0 版分发。
    // (请参阅随附文件 LICENSE.md 或访问 https://boost.ac.cn/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() { }
    定义用于执行不同类型断言的宏。
    定义 boost::hana::equal。
    constexpr auto equal
    返回一个表示 x 是否等于 y 的逻辑值。
    定义: equal.hpp:64
    constexpr auto not_equal
    返回一个表示 x 是否不等于 y 的 Logical。
    定义: not_equal.hpp:54
    #define BOOST_HANA_CONSTANT_CHECK(...)
    等同于 BOOST_HANA_CONSTANT_ASSERT,但不受 BOOST_HANA_CONFIG_DISABLE_ASSERTI... 的影响。
    定义: assert.hpp:239
    为 Hana 使用而适配 boost::mpl::list。
    包含库中所有内容的命名空间。
    定义: accessors.hpp:20
    定义了 boost::hana::not_equal。
  2. Foldable (可折叠)
    折叠 MPL 列表等同于将其作为一个 Sequence 进行折叠。
    // 版权 Louis Dionne 2013-2017
    // 根据 Boost 软件许可证第 1.0 版分发。
    // (请参阅随附文件 LICENSE.md 或访问 https://boost.ac.cn/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() { }
    适配 std::integral_constant 以便与 Hana 一起使用。
    定义了 boost::hana::fold_left。
    constexpr auto count
    返回结构中与给定值相等的元素数量。
    定义: count.hpp:41
    constexpr auto if_
    根据条件有条件地返回两个值之一。
    定义: if.hpp:41
    定义了 boost::hana::if_。
    定义 boost::hana::integral_constant。
    定义 boost::hana::plus。
    定义 boost::hana::type 和相关工具。
  3. Iterable (可迭代)
    遍历 MPL 列表就像遍历其中包含的每个类型一样,就好像它是一个 Sequence
    // 版权 Louis Dionne 2013-2017
    // 根据 Boost 软件许可证第 1.0 版分发。
    // (请参阅随附文件 LICENSE.md 或访问 https://boost.ac.cn/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() { }
    定义了 boost::hana::drop_front。
    定义了 boost::hana::drop_while。
    定义 boost::hana::front。
    constexpr auto front
    返回非空可迭代对象的第一个元素。
    定义: front.hpp:32
    constexpr auto drop_while
    从可迭代对象中删除元素,直到(但不包括)谓词不满足的第一个元素...
    定义: drop_while.hpp:44
    constexpr auto drop_front
    从一个可迭代对象中删除前 n 个元素,并返回剩余的元素。
    定义: drop_front.hpp:47
  4. Searchable (可搜索)
    MPL 列表可以像包含 hana::type 的元组一样进行搜索。
    // 版权 Louis Dionne 2013-2017
    // 根据 Boost 软件许可证第 1.0 版分发。
    // (请参阅随附文件 LICENSE.md 或访问 https://boost.ac.cn/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() { }
    定义了 boost::hana::find。
    定义了 boost::hana::find_if。
    constexpr auto find
    在结构中查找与给定键关联的值。
    定义: find.hpp:44
    constexpr auto find_if
    查找与满足谓词的第一个键关联的值。
    定义: find_if.hpp:41
    定义了 boost::hana::optional。

从任意 <tt>Foldable</tt> 转换

MPL 列表可以从任何 Foldable 创建。更确切地说,对于一个线性化为 [x1, ..., xn]Foldable xs

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

其中 tkxk 的类型,或者如果 xkhana::type,则为 xk 所包含的类型。

警告
mpl::list 的大小限制会通过此转换工具继承,因此尝试转换包含超过 BOOST_MPL_LIMIT_LIST_SIZE 个元素的 Foldable 是一个错误。
// 版权 Louis Dionne 2013-2017
// 根据 Boost 软件许可证第 1.0 版分发。
// (请参阅随附文件 LICENSE.md 或访问 https://boost.ac.cn/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() { }
定义了 boost::hana::to 和相关工具。
定义 boost::hana::tuple。