summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/test/type/is_valid.cpp
blob: 2f2abcd35db3e3828f583ecd828ee5c358a1210d (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// 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/equal.hpp>
#include <boost/hana/not.hpp>
#include <boost/hana/traits.hpp>
#include <boost/hana/type.hpp>

#include <support/tracked.hpp>

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


struct undefined { };

struct static_nested_member { static const int member = 1; };
struct static_nested_member_array { static int member[3]; };
struct nested_template_struct { template <typename ...> struct nested; };
struct nested_template_alias { template <typename ...> using nested = void; };

int main() {
    // Check for a non-static member
    {
        struct yes { int member; };
        struct no { };

        auto from_type = hana::is_valid([](auto t) -> decltype(
            hana::traits::declval(t).member
        ) { });
        BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));

        auto from_object = hana::is_valid([](auto&& t) -> decltype(
            t.member
        ) { });
        BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
    }

    // Check for a non-static member function
    {
        struct yes { int member() const; };
        struct no { };

        auto from_type = hana::is_valid([](auto t) -> decltype(
            hana::traits::declval(t).member()
        ) { });
        BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));

        auto from_object = hana::is_valid([](auto&& t) -> decltype(
            t.member()
        ) { });
        BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
    }

    // Check for a static member
    {
        using yes = static_nested_member;
        struct no { };

        auto from_type = hana::is_valid([](auto t) -> decltype(
            decltype(t)::type::member
        ) { });
        BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));

        auto from_object = hana::is_valid([](auto&& t) -> decltype(
            std::remove_reference_t<decltype(t)>::member
        ) { });
        BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
    }

    // Check for a nested type
    {
        struct yes { using nested = void; };
        struct no { };

        auto from_type = hana::is_valid([](auto t) -> decltype(hana::type_c<
            typename decltype(t)::type::nested
        >) { });
        BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));

        auto from_object = hana::is_valid([](auto&& t) -> decltype(hana::type_c<
            typename std::remove_reference_t<decltype(t)>::nested
        >) { });
        BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
    }

    // Check for a nested template
    {
        { // template struct
        using yes = nested_template_struct;
        struct no { };

        auto from_type = hana::is_valid([](auto t) -> decltype(hana::template_<
            decltype(t)::type::template nested
        >) { });
        BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));

        auto from_object = hana::is_valid([](auto&& t) -> decltype(hana::template_<
            std::remove_reference_t<decltype(t)>::template nested
        >) { });
        BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
        }

        { // template alias
        using yes = nested_template_alias;
        struct no { };

        auto from_type = hana::is_valid([](auto t) -> decltype(hana::template_<
            decltype(t)::type::template nested
        >) { });
        BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));

        auto from_object = hana::is_valid([](auto&& t) -> decltype(hana::template_<
            std::remove_reference_t<decltype(t)>::template nested
        >) { });
        BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
        }
    }

    // Make sure that checking for a nested static or non-static member
    // works even when the type of that member is an array type or
    // something that can't be returned from a function.
    {
        { // non-static member
        struct yes { int member[3]; };
        struct no { };

        auto from_type = hana::is_valid([](auto t) -> decltype(
            (void)hana::traits::declval(t).member
        ) { });
        BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));

        auto from_object = hana::is_valid([](auto&& t) -> decltype(
            (void)t.member
        ) { });
        BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
        }

        { // static member
        using yes = static_nested_member_array;
        struct no { };

        auto from_type = hana::is_valid([](auto t) -> decltype(
            (void)decltype(t)::type::member
        ) { });
        BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));

        auto from_object = hana::is_valid([](auto&& t) -> decltype(
            (void)std::remove_reference_t<decltype(t)>::member
        ) { });
        BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
        }
    }

    // Make sure the result of a `is_valid` function is constexpr
    // even when called on non-constexpr arguments.
    {
        int i;
        auto f = hana::is_valid([](auto) { });
        constexpr auto result = f(i);
        (void)result;
    }

    // Make sure `is_valid` works with non-PODs.
    {
        hana::is_valid(undefined{})(Tracked{1});
        hana::is_valid([t = Tracked{1}](auto) { return 1; })(Tracked{1});
    }

    // Check `is_valid` with a nullary function.
    {
        auto f = [](auto ...x) { (void)sizeof...(x); /* -Wunused-param */ };
        auto g = [](auto ...x) -> char(*)[sizeof...(x)] { };
        BOOST_HANA_CONSTANT_CHECK(hana::is_valid(f)());
        BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::is_valid(g)()));
    }

    // Call `is_valid` in the non-curried form.
    {
        struct yes { int member; };
        struct no { };

        auto f = [](auto&& t) -> decltype(t.member) { };

        BOOST_HANA_CONSTANT_CHECK(hana::is_valid(f, yes{}));
        BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::is_valid(f, no{})));
    }
}