]>
Commit | Line | Data |
---|---|---|
b32b8144 | 1 | // Copyright Louis Dionne 2013-2017 |
7c673cae FG |
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) | |
4 | ||
5 | #include <boost/hana/assert.hpp> | |
6 | #include <boost/hana/chain.hpp> | |
7 | #include <boost/hana/config.hpp> | |
8 | #include <boost/hana/equal.hpp> | |
9 | #include <boost/hana/optional.hpp> | |
10 | namespace hana = boost::hana; | |
11 | ||
12 | ||
13 | BOOST_HANA_CONSTEXPR_LAMBDA auto deref = [](auto x) -> decltype(*x) { | |
14 | return *x; | |
15 | }; | |
16 | ||
17 | BOOST_HANA_CONSTEXPR_LAMBDA auto age = [](auto x) -> decltype(x.age) { | |
18 | return x.age; | |
19 | }; | |
20 | ||
21 | BOOST_HANA_CONSTEXPR_LAMBDA auto f = [](auto x) { | |
22 | return hana::chain(hana::sfinae(deref)(x), hana::sfinae(age)); | |
23 | }; | |
24 | ||
25 | struct Person { | |
26 | unsigned int age; | |
27 | // ... | |
28 | }; | |
29 | ||
30 | int main() { | |
b32b8144 | 31 | constexpr Person john{30}; |
7c673cae FG |
32 | |
33 | // Can't dereference a non-pointer. | |
34 | BOOST_HANA_CONSTANT_CHECK(f(john) == hana::nothing); | |
35 | ||
36 | // `int` has no member named `age`. | |
37 | BOOST_HANA_CONSTANT_CHECK(f(1) == hana::nothing); | |
38 | ||
39 | // All is good. | |
40 | BOOST_HANA_CONSTEXPR_CHECK(f(&john) == hana::just(30u)); | |
41 | } |