Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / hana / example / eval_if.cpp
1 // Copyright Louis Dionne 2013-2017
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/config.hpp>
7 #include <boost/hana/equal.hpp>
8 #include <boost/hana/eval_if.hpp>
9 #include <boost/hana/lazy.hpp>
10 #include <boost/hana/traits.hpp>
11 #include <boost/hana/type.hpp>
12 namespace hana = boost::hana;
13
14
15 // eval_if with heterogeneous branches and a Constant condition
16 BOOST_HANA_CONSTEXPR_LAMBDA auto safe_make_unsigned = [](auto t) {
17     return hana::eval_if(hana::traits::is_integral(t),
18         hana::make_lazy(hana::traits::make_unsigned)(t),
19         hana::make_lazy(t)
20     );
21 };
22
23 BOOST_HANA_CONSTANT_CHECK(safe_make_unsigned(hana::type_c<void>) == hana::type_c<void>);
24 BOOST_HANA_CONSTANT_CHECK(safe_make_unsigned(hana::type_c<int>) == hana::type_c<unsigned int>);
25
26
27 // eval_if with homogeneous branches and a constexpr or runtime condition
28 BOOST_HANA_CONSTEXPR_LAMBDA auto safe_divide = [](auto x, auto y) {
29     return hana::eval_if(y == 0,
30         [=](auto) { return 0; },
31         [=](auto _) { return _(x) / y; }
32     );
33 };
34
35 int main() {
36     BOOST_HANA_CONSTEXPR_CHECK(safe_divide(6, 3) == 2);
37     BOOST_HANA_CONSTEXPR_CHECK(safe_divide(6, 0) == 0);
38 }