summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/example/sequence/monad.types.cpp
blob: 27ea46da87a250cc932758ad7e562f2423c957f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 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 <boost/hana/assert.hpp>
#include <boost/hana/chain.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/for_each.hpp>
#include <boost/hana/traits.hpp>
#include <boost/hana/tuple.hpp>
#include <boost/hana/type.hpp>

#include <type_traits>
#include <utility>
namespace hana = boost::hana;


// Using the `tuple` Monad, we generate all the possible combinations of
// cv-qualifiers and reference qualifiers. Then, we use the `optional`
// Monad to make sure that our generic function can be called with
// arguments of any of those types.

// cv_qualifiers : type -> tuple(type)
auto cv_qualifiers = [](auto t) {
    return hana::make_tuple(
        t,
        hana::traits::add_const(t),
        hana::traits::add_volatile(t),
        hana::traits::add_volatile(hana::traits::add_const(t))
    );
};

// ref_qualifiers : type -> tuple(type)
auto ref_qualifiers = [](auto t) {
    return hana::make_tuple(
        hana::traits::add_lvalue_reference(t),
        hana::traits::add_rvalue_reference(t)
    );
};

auto possible_args = cv_qualifiers(hana::type_c<int>) | ref_qualifiers;

BOOST_HANA_CONSTANT_CHECK(
    possible_args == hana::make_tuple(
                        hana::type_c<int&>,
                        hana::type_c<int&&>,
                        hana::type_c<int const&>,
                        hana::type_c<int const&&>,
                        hana::type_c<int volatile&>,
                        hana::type_c<int volatile&&>,
                        hana::type_c<int const volatile&>,
                        hana::type_c<int const volatile&&>
                    )
);

struct some_function {
    template <typename T>
    void operator()(T&&) const { }
};

int main() {
    hana::for_each(possible_args, [](auto t) {
        using T = typename decltype(t)::type;
        static_assert(decltype(hana::is_valid(some_function{})(std::declval<T>())){},
        "some_function should be callable with any type of argument");
    });
}