int main() { // with state { auto xs = hana::make_tuple(1, 2, 3); int state = 99; int& one = hana::fold_right(xs, state, [](int& i, int&) -> int& { return i; }); BOOST_HANA_RUNTIME_CHECK(one == 1); one = 10; BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(xs) == 10); } // without state { auto xs = hana::make_tuple(1, 2, 3); int& one = hana::fold_right(xs, [](int& i, int&) -> int& { return i; }); BOOST_HANA_RUNTIME_CHECK(one == 1); one = 10; BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(xs) == 10); } }
int main() { hana::tuple<int, char, double, std::string> ts{1, '2', 3.3, "abcd"}; BOOST_HANA_RUNTIME_CHECK(ts == hana::make_tuple(1, '2', 3.3, std::string{"abcd"})); // std::string has no notion of tag, but it still works with make<> std::string foo{"foo"}; BOOST_HANA_RUNTIME_CHECK(hana::make<std::string>("foo") == foo); }
int main() { auto f = ::overload_linearly( [](std::string s) { return s + "abcd"; }, [](int i) { return i + 1; }, [](double f) { return f + 2; } ); BOOST_HANA_RUNTIME_CHECK(f(1) == hana::just(1 + 1)); BOOST_HANA_RUNTIME_CHECK(f(2.3) == hana::just(static_cast<int>(2.3) + 1)); }
int main() { auto xs = hana::make_map(hana::make_pair(hana::int_c<0>, hana::make_map(hana::make_pair(hana::int_c<1>, hana::make_map(hana::make_pair(hana::int_c<2>, hana::make_map(hana::make_pair(hana::int_c<3>, 10)))))))); int& i = traverse(xs, hana::range_c<int, 0, 4>); BOOST_HANA_RUNTIME_CHECK(i == 10); i = 99; BOOST_HANA_RUNTIME_CHECK(traverse(xs, hana::range_c<int, 0, 4>) == 99); }
int main() { auto m = hana::make_map( hana::make_pair(hana::type_c<int>, std::string{"int"}), hana::make_pair(hana::int_c<3>, std::string{"3"}) ); BOOST_HANA_RUNTIME_CHECK(hana::at_key(m, hana::type_c<int>) == "int"); // usage as operator[] BOOST_HANA_RUNTIME_CHECK(m[hana::type_c<int>] == "int"); BOOST_HANA_RUNTIME_CHECK(m[hana::int_c<3>] == "3"); }
int main() { int i = 0, j = 1, k = 2; ref_tuple<int&, int&, int&> refs = hana::make<RefTuple>(i, j, k); hana::at_c<0>(refs) = 3; BOOST_HANA_RUNTIME_CHECK(i == 3); BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::is_empty(refs))); ref_tuple<int&, int&> tail = hana::drop_front(refs); hana::at_c<0>(tail) = 4; BOOST_HANA_RUNTIME_CHECK(j == 4); }
int main() { // tuple tuple<int, char, double> ts{1, '2', 3.3}; // make_tuple make_tuple(); make_tuple(1, '2', 3.3); // get BOOST_HANA_RUNTIME_CHECK(get<0>(ts) == 1); BOOST_HANA_RUNTIME_CHECK(get<1>(ts) == '2'); BOOST_HANA_RUNTIME_CHECK(get<2>(ts) == 3.3); }
int main() { // We use a sorted container because the order in which the functions // are called is unspecified. std::set<int> before, after; auto xs = hana::make_tuple(1, 2, 3) | hana::tap<hana::tuple_tag>([&](int x) { before.insert(x); }) | [](auto x) { return hana::make_tuple(x, -x); } | hana::tap<hana::tuple_tag>([&](int x) { after.insert(x); }); BOOST_HANA_RUNTIME_CHECK(before == std::set<int>{1, 2, 3}); BOOST_HANA_RUNTIME_CHECK(after == std::set<int>{1, -1, 2, -2, 3, -3}); BOOST_HANA_RUNTIME_CHECK(xs == hana::make_tuple(1, -1, 2, -2, 3, -3)); }
int main() { Person john{"John", 30}, bob{"Bob", 40}; BOOST_HANA_RUNTIME_CHECK(hana::equal(john, john)); BOOST_HANA_RUNTIME_CHECK(hana::not_equal(john, bob)); BOOST_HANA_RUNTIME_CHECK(hana::find(john, BOOST_HANA_STRING("name")) == hana::just("John")); BOOST_HANA_RUNTIME_CHECK(hana::find(john, BOOST_HANA_STRING("age")) == hana::just(30)); BOOST_HANA_CONSTANT_CHECK(hana::find(john, BOOST_HANA_STRING("foo")) == hana::nothing); BOOST_HANA_RUNTIME_CHECK(hana::to_tuple(john) == hana::make_tuple( hana::make_pair(BOOST_HANA_STRING("name"), "John"), hana::make_pair(BOOST_HANA_STRING("age"), 30) )); }
int main() { // with initial state BOOST_HANA_RUNTIME_CHECK(hana::scan_right(hana::make_tuple(1, "2", '3'), 4, f) == hana::make_tuple( "f(1, f(2, f(3, 4)))", "f(2, f(3, 4))", "f(3, 4)", 4 )); // without initial state BOOST_HANA_RUNTIME_CHECK(hana::scan_right(hana::make_tuple(1, "2", '3'), f) == hana::make_tuple( "f(1, f(2, 3))", "f(2, 3)", '3' )); }
int main() { { using T = hana::tuple<std::unique_ptr<int>>; T t(std::unique_ptr<int>(new int(3))); std::unique_ptr<int> p = hana::at_c<0>(std::move(t)); BOOST_HANA_RUNTIME_CHECK(*p == 3); } // make sure we don't double-move and do other weird stuff { hana::tuple<Tracked, Tracked, Tracked> xs{ Tracked{1}, Tracked{2}, Tracked{3} }; Tracked a = hana::at_c<0>(std::move(xs)); (void)a; Tracked b = hana::at_c<1>(std::move(xs)); (void)b; Tracked c = hana::at_c<2>(std::move(xs)); (void)c; } // test with nested closures { using Inner = hana::tuple<Tracked, Tracked>; hana::tuple<Inner> xs{Inner{Tracked{1}, Tracked{2}}}; Tracked a = hana::at_c<0>(hana::at_c<0>(std::move(xs))); (void)a; Tracked b = hana::at_c<1>(hana::at_c<0>(std::move(xs))); (void)b; } }
int main() { // with initial state BOOST_HANA_RUNTIME_CHECK(hana::scan_left(hana::make_tuple(2, "3", '4'), 1, f) == hana::make_tuple( 1, "f(1, 2)", "f(f(1, 2), 3)", "f(f(f(1, 2), 3), 4)" )); // without initial state BOOST_HANA_RUNTIME_CHECK(hana::scan_left(hana::make_tuple(1, "2", '3'), f) == hana::make_tuple( 1, "f(1, 2)", "f(f(1, 2), 3)" )); }
int main() { static_assert(hana::just('x').value() == 'x', ""); BOOST_HANA_CONSTANT_CHECK(*hana::just(hana::type_c<int>) == hana::type_c<int>); BOOST_HANA_RUNTIME_CHECK(hana::just(std::string{"abcd"})->size() == 4); // hana::nothing.value(); // compile-time error }
int main() { { std::ostringstream ss; ss << hana::experimental::print( hana::just(hana::int_c<5>) ); BOOST_HANA_RUNTIME_CHECK(ss.str() == "just(5)"); } { std::ostringstream ss; ss << hana::experimental::print( hana::nothing ); BOOST_HANA_RUNTIME_CHECK(ss.str() == "nothing"); } }
int main() { std::stringstream ss; hana::for_each(hana::make_tuple(0, '1', "234", 5.5), [&](auto x) { ss << x << ' '; }); BOOST_HANA_RUNTIME_CHECK(ss.str() == "0 1 234 5.5 "); }
int main() { BOOST_HANA_RUNTIME_CHECK( hana::to<std::vector<int>>(std::vector<float> {1.1, 2.2, 3.3}) == std::vector<int> {1, 2, 3} ); static_assert(!hana::is_embedded<std::vector<float>, std::vector<int>> {}, ""); }
int main() { auto f = [=](std::string s, auto element) { return "f(" + s + ", " + to_string(element) + ")"; }; // with an initial state BOOST_HANA_RUNTIME_CHECK( hana::fold_left(hana::make_tuple(2, '3', 4, 5.0), "1", f) == "f(f(f(f(1, 2), 3), 4), 5)" ); // without initial state BOOST_HANA_RUNTIME_CHECK( hana::fold_left(hana::make_tuple("1", 2, '3', 4, 5.0), f) == "f(f(f(f(1, 2), 3), 4), 5)" ); }
int main() { auto f = [=](auto element, std::string s) { return "f(" + to_string(element) + ", " + s + ")"; }; // with an initial state BOOST_HANA_RUNTIME_CHECK( hana::fold_right(hana::make_tuple(1, '2', 3.0, 4), "5", f) == "f(1, f(2, f(3, f(4, 5))))" ); // without initial state BOOST_HANA_RUNTIME_CHECK( hana::fold_right(hana::make_tuple(1, '2', 3.0, 4, "5"), f) == "f(1, f(2, f(3, f(4, 5))))" ); }
int main() { auto f = [=](std::string s, auto element) { return "f(" + s + ", " + to_string(element) + ")"; }; // With an initial state BOOST_HANA_RUNTIME_CHECK( hana::reverse_fold(hana::make_tuple(1, '2', 3.0, 4), "5", f) == "f(f(f(f(5, 4), 3), 2), 1)" ); // Without an initial state BOOST_HANA_RUNTIME_CHECK( hana::reverse_fold(hana::make_tuple(1, '2', 3.0, 4, "5"), f) == "f(f(f(f(5, 4), 3), 2), 1)" ); }
int main() { std::tuple<int, long, double> tuple{1, 2l, 3.3}; auto sum = hana::unpack(std::integer_sequence<int, 0, 1, 2>{}, [&](auto ...i) { // the `i`s are `std::integral_constant<int, ...>`s return add(std::get<decltype(i)::value>(tuple)...); }); BOOST_HANA_RUNTIME_CHECK(sum == 6.3); }
int main() { { auto t0 = hana::make_map(); auto t_implicit = t0; auto t_explicit(t0); (void)t_explicit; (void)t_implicit; } { auto t0 = hana::make_map(hana::make_pair(hana::int_c<2>, hana::int_c<20>)); auto t_implicit = t0; auto t_explicit(t0); (void)t_implicit; (void)t_explicit; } { auto t0 = hana::make_map(hana::make_pair(hana::int_c<2>, hana::int_c<20>), hana::make_pair(hana::int_c<3>, hana::int_c<30>)); auto t_implicit = t0; auto t_explicit(t0); (void)t_implicit; (void)t_explicit; } { auto t0 = hana::make_map(hana::make_pair(hana::int_c<2>, hana::int_c<20>), hana::make_pair(hana::int_c<3>, hana::int_c<30>), hana::make_pair(hana::type_c<void>, hana::type_c<void*>)); auto t_implicit = t0; auto t_explicit(t0); (void)t_implicit; (void)t_explicit; } { constexpr auto t0 = hana::make_map( hana::make_pair(hana::int_c<2>, hana::int_c<20>), hana::make_pair(hana::int_c<3>, hana::int_c<30>), hana::make_pair(hana::type_c<void>, hana::type_c<void*>)); constexpr auto t_implicit = t0; constexpr auto t_explicit(t0); (void)t_implicit; (void)t_explicit; } { auto t0 = hana::make_map(hana::make_pair(hana::int_c<2>, std::string{"abcdef"})); auto copy = t0; BOOST_HANA_RUNTIME_CHECK( copy == hana::make_map(hana::make_pair(hana::int_c<2>, std::string{"abcdef"})) ); } }
int main() { hana::tuple<Cat, Fish, Dog, Fish> animals{ Cat{"Garfield"}, Fish{"Jaws"}, Dog{"Beethoven"}, Fish{"Nemo"} }; auto mammals = hana::remove_if(animals, [](auto const& a) { return hana::typeid_(a) == hana::type<Fish>{}; }); BOOST_HANA_RUNTIME_CHECK(mammals == hana::make_tuple(Cat{"Garfield"}, Dog{"Beethoven"})); }
int main() { boost::tuple<Fish, Cat, Dog> animals{{"Nemo"}, {"Garfield"}, {"Snoopy"}}; hana::front(animals).name = "Moby Dick"; auto names = hana::transform(animals, [](auto a) { return a.name; }); BOOST_HANA_RUNTIME_CHECK(hana::equal( names, boost::make_tuple("Moby Dick", "Garfield", "Snoopy") )); }
int main() { { std::stringstream ss; vector0 v0; print(ss, v0); BOOST_HANA_RUNTIME_CHECK(ss.str() == "[]"); } { std::stringstream ss; vector1<int> v1{1}; print(ss, v1); BOOST_HANA_RUNTIME_CHECK(ss.str() == "[1]"); } { std::stringstream ss; vector2<int, char> v2{1, 'x'}; print(ss, v2); BOOST_HANA_RUNTIME_CHECK(ss.str() == "[1, x]"); } }
int main() { fusion::vector<Fish, Cat, Dog> animals{Fish{"Nemo"}, Cat{"Garfield"}, Dog{"Snoopy"}}; hana::front(animals).name = "Moby Dick"; auto names = hana::transform(animals, [](auto a) { return a.name; }); BOOST_HANA_RUNTIME_CHECK(hana::equal( names, fusion::make_vector("Moby Dick", "Garfield", "Snoopy") )); }
int main() { { std::ostringstream ss; ss << hana::experimental::print( hana::make_map() ); BOOST_HANA_RUNTIME_CHECK(ss.str() == "{}"); } { std::ostringstream ss; ss << hana::experimental::print( hana::make_map(hana::make_pair(hana::int_c<1>, 'x')) ); BOOST_HANA_RUNTIME_CHECK(ss.str() == "{1 => x}"); } { std::ostringstream ss; ss << hana::experimental::print( hana::make_map(hana::make_pair(hana::int_c<1>, 'x'), hana::make_pair(hana::int_c<2>, 'y')) ); BOOST_HANA_RUNTIME_CHECK(ss.str() == "{1 => x, 2 => y}"); } { std::ostringstream ss; ss << hana::experimental::print( hana::make_map(hana::make_pair(hana::int_c<1>, 'x'), hana::make_pair(hana::int_c<2>, 'y'), hana::make_pair(hana::int_c<3>, 'z')) ); BOOST_HANA_RUNTIME_CHECK(ss.str() == "{1 => x, 2 => y, 3 => z}"); } }
int main() { { using T0 = hana::tuple<double>; using T1 = hana::tuple<int>; T0 t0(2.5); T1 t1; t1 = t0; BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t1) == 2); } { using T0 = hana::tuple<double, char>; using T1 = hana::tuple<int, int>; T0 t0(2.5, 'a'); T1 t1; t1 = t0; BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t1) == 2); BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t1) == int('a')); } { using T0 = hana::tuple<double, char, D>; using T1 = hana::tuple<int, int, B>; T0 t0(2.5, 'a', D(3)); T1 t1; t1 = t0; BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t1) == 2); BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t1) == int('a')); BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(t1).id_ == 3); } { D d(3); D d2(2); using T0 = hana::tuple<double, char, D&>; using T1 = hana::tuple<int, int, B&>; T0 t0(2.5, 'a', d2); T1 t1(1.5, 'b', d); t1 = t0; BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t1) == 2); BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t1) == int('a')); BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(t1).id_ == 2); } }
int main() { int counter = 0; hana::repeat(constant<3> {}, [&] { ++counter; }); BOOST_HANA_RUNTIME_CHECK(counter == 3); // Try with a normal function. hana::repeat(constant<3> {}, function); // Try with a function pointer. hana::repeat(constant<3> {}, static_cast<void(*)()>(function)); // Make sure we don't read from a non-constexpr variable. constant<3> three{}; hana::repeat(three, [] {}); }
int main() { { auto t0 = hana::make_map(); auto t_implicit = std::move(t0); auto t_explicit(std::move(t_implicit)); (void)t_explicit; (void)t_implicit; } { auto t0 = hana::make_map(hana::make_pair(TrackedMoveOnly<1>{}, TrackedMoveOnly<10>{})); auto t_implicit = std::move(t0); auto t_explicit(std::move(t_implicit)); (void)t_implicit; (void)t_explicit; } { auto t0 = hana::make_map(hana::make_pair(TrackedMoveOnly<1>{}, TrackedMoveOnly<10>{}), hana::make_pair(TrackedMoveOnly<2>{}, TrackedMoveOnly<20>{})); auto t_implicit = std::move(t0); auto t_explicit(std::move(t_implicit)); (void)t_implicit; (void)t_explicit; } { auto t0 = hana::make_map(hana::make_pair(TrackedMoveOnly<1>{}, TrackedMoveOnly<10>{}), hana::make_pair(TrackedMoveOnly<2>{}, TrackedMoveOnly<20>{}), hana::make_pair(TrackedMoveOnly<3>{}, TrackedMoveOnly<30>{})); auto t_implicit = std::move(t0); auto t_explicit(std::move(t_implicit)); (void)t_implicit; (void)t_explicit; } { auto t0 = hana::make_map(hana::make_pair(hana::int_c<2>, std::string{"abcdef"})); auto moved = std::move(t0); BOOST_HANA_RUNTIME_CHECK( moved == hana::make_map(hana::make_pair(hana::int_c<2>, std::string{"abcdef"})) ); } }
int main() { { using T = hana::tuple<>; T t0; T t = std::move(t0); (void)t; } { using T = hana::tuple<MoveOnly>; T t0(MoveOnly(0)); T t = std::move(t0); (void)t; BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 0); } { using T = hana::tuple<MoveOnly, MoveOnly>; T t0(MoveOnly(0), MoveOnly(1)); T t = std::move(t0); (void)t; BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 0); BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == 1); } { using T = hana::tuple<MoveOnly, MoveOnly, MoveOnly>; T t0(MoveOnly(0), MoveOnly(1), MoveOnly(2)); T t = std::move(t0); (void)t; BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 0); BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == 1); BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(t) == 2); } { // Check for SFINAE-friendliness static_assert(!std::is_constructible< hana::tuple<MoveOnly>, MoveOnly const& >{}, ""); static_assert(!std::is_constructible< hana::tuple<MoveOnly>, MoveOnly& >{}, ""); static_assert(std::is_constructible< hana::tuple<MoveOnly>, MoveOnly >{}, ""); static_assert(std::is_constructible< hana::tuple<MoveOnly>, MoveOnly&& >{}, ""); } }