1 // Copyright Louis Dionne 2013-2016
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
5 #ifndef BOOST_HANA_TEST_AUTO_FOR_EACH_HPP
6 #define BOOST_HANA_TEST_AUTO_FOR_EACH_HPP
8 #include <boost/hana/assert.hpp>
9 #include <boost/hana/for_each.hpp>
11 #include "test_case.hpp"
12 #include <laws/base.hpp>
17 TestCase test_for_each{[]{
18 namespace hana = boost::hana;
19 using hana::test::ct_eq;
21 // Make sure the function is applied in left-to-right order.
23 auto check = [](auto ...xs) {
24 std::vector<int> seen{};
25 hana::for_each(MAKE_TUPLE(xs...), [&](int x) {
28 BOOST_HANA_RUNTIME_CHECK(seen == std::vector<int>{xs...});
39 // Make sure the function is never called when the sequence is empty.
42 hana::for_each(MAKE_TUPLE(), undefined{});
45 // Make sure it works with heterogeneous sequences.
47 hana::for_each(MAKE_TUPLE(ct_eq<0>{}), [](auto) { });
48 hana::for_each(MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}), [](auto) { });
49 hana::for_each(MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}, ct_eq<2>{}), [](auto) { });
50 hana::for_each(MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}, ct_eq<2>{}, ct_eq<3>{}), [](auto) { });
53 // Make sure for_each is constexpr when used with a constexpr function
54 // and constexpr arguments. This used not to be the case.
55 #ifndef MAKE_TUPLE_NO_CONSTEXPR
57 struct f { constexpr void operator()(int) const { } };
58 constexpr int i = (hana::for_each(MAKE_TUPLE(1, 2, 3), f{}), 0);
64 #endif // !BOOST_HANA_TEST_AUTO_FOR_EACH_HPP