// 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace fusion = boost::fusion; namespace mpl = boost::mpl; namespace hana = boost::hana; using namespace hana::literals; template struct storage { char weight[n]; }; int main() { { //! [tuple] auto types = hana::make_tuple(hana::type_c, hana::type_c, hana::type_c); auto char_ref = types[1_c]; BOOST_HANA_CONSTANT_CHECK(char_ref == hana::type_c); //! [tuple] }{ //! [filter.MPL] using types = mpl::vector; using ts = mpl::copy_if, std::is_reference>>::type; // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // placeholder expression static_assert(mpl::equal>::value, ""); //! [filter.MPL] }{ using hana::traits::is_pointer; // the traits namespace was not introduced using hana::traits::is_reference; // yet, so we use unqualified names for now //! [filter.Hana] auto types = hana::tuple_t; auto ts = hana::filter(types, [](auto t) { return is_pointer(t) || is_reference(t); }); BOOST_HANA_CONSTANT_CHECK(ts == hana::tuple_t); //! [filter.Hana] }{ //! [single_library.then] // types (MPL) using types = mpl::vector; using ts = mpl::copy_if, std::is_reference>>::type; // values (Fusion) auto values = fusion::make_vector(1, 'c', nullptr, 3.5); auto vs = fusion::filter_if>(values); //! [single_library.then] static_assert(mpl::equal>::value, ""); BOOST_HANA_RUNTIME_CHECK(vs == fusion::make_vector(1, 'c')); }{ using hana::traits::is_pointer; using hana::traits::is_reference; using hana::traits::is_integral; //! [single_library.Hana] // types auto types = hana::tuple_t; auto ts = hana::filter(types, [](auto t) { return is_pointer(t) || is_reference(t); }); // values auto values = hana::make_tuple(1, 'c', nullptr, 3.5); auto vs = hana::filter(values, [](auto const& t) { return is_integral(hana::typeid_(t)); }); //! [single_library.Hana] BOOST_HANA_CONSTANT_CHECK(ts == hana::tuple_t); BOOST_HANA_RUNTIME_CHECK(vs == hana::make_tuple(1, 'c')); }{ //! [make_map.Fusion] auto map = fusion::make_map( "char", "int", "long", "float", "double", "void" ); std::string Int = fusion::at_key(map); BOOST_HANA_RUNTIME_CHECK(Int == "int"); //! [make_map.Fusion] }{ //! [make_map.Hana] auto map = hana::make_map( hana::make_pair(hana::type_c, "char"), hana::make_pair(hana::type_c, "int"), hana::make_pair(hana::type_c, "long"), hana::make_pair(hana::type_c, "float"), hana::make_pair(hana::type_c, "double") ); std::string Int = map[hana::type_c]; BOOST_HANA_RUNTIME_CHECK(Int == "int"); //! [make_map.Hana] }{ using hana::traits::add_pointer; //! [skip_first_step] auto types = hana::tuple_t; // first step skipped auto pointers = hana::transform(types, [](auto t) { return add_pointer(t); }); //! [skip_first_step] BOOST_HANA_CONSTANT_CHECK(pointers == hana::tuple_t); }{ //! [traits] BOOST_HANA_CONSTANT_CHECK(hana::traits::add_pointer(hana::type_c) == hana::type_c); BOOST_HANA_CONSTANT_CHECK(hana::traits::common_type(hana::type_c, hana::type_c) == hana::type_c); BOOST_HANA_CONSTANT_CHECK(hana::traits::is_integral(hana::type_c)); auto types = hana::tuple_t; BOOST_HANA_CONSTANT_CHECK(hana::all_of(types, hana::traits::is_integral)); //! [traits] }{ //! [extent] auto extent = [](auto t, auto n) { return std::extent{}; }; BOOST_HANA_CONSTANT_CHECK(extent(hana::type_c, hana::int_c<1>) == hana::size_c<0>); BOOST_HANA_CONSTANT_CHECK(extent(hana::type_c, hana::int_c<1>) == hana::size_c<2>); //! [extent] } } namespace mpl_based { //! [smallest.MPL] template struct smallest : mpl::deref< typename mpl::min_element< mpl::vector, mpl::less, mpl::sizeof_> >::type > { }; template using smallest_t = typename smallest::type; static_assert(std::is_same< smallest_t, char >::value, ""); //! [smallest.MPL] static_assert(std::is_same< smallest_t, storage<1>, storage<2>>, storage<1> >::value, ""); } // end namespace mpl_based namespace hana_based { //! [smallest.Hana] template auto smallest = hana::minimum(hana::make_tuple(hana::type_c...), [](auto t, auto u) { return hana::sizeof_(t) < hana::sizeof_(u); }); template using smallest_t = typename decltype(smallest)::type; static_assert(std::is_same< smallest_t, char >::value, ""); //! [smallest.Hana] static_assert(std::is_same< smallest_t, storage<1>, storage<2>>, storage<1> >::value, ""); } // end namespace hana_based namespace metafunction1 { //! [metafunction1] template