| 1 | // Copyright Louis Dionne 2013-2022 |
| 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/concept/constant.hpp> |
| 6 | #include <boost/hana/concept/integral_constant.hpp> |
| 7 | #include <boost/hana/core/when.hpp> |
| 8 | #include <boost/hana/fwd/core/to.hpp> |
| 9 | #include <boost/hana/value.hpp> |
| 10 | namespace hana = boost::hana; |
| 11 | |
| 12 | |
| 13 | // Define a simple model of IntegralConstant |
| 14 | struct constant_tag { using value_type = int; }; |
| 15 | template <int i> |
| 16 | struct constant { |
| 17 | static constexpr int value = i; |
| 18 | using hana_tag = constant_tag; |
| 19 | }; |
| 20 | |
| 21 | namespace boost { namespace hana { |
| 22 | template <> |
| 23 | struct IntegralConstant<constant_tag> { |
| 24 | static constexpr bool value = true; |
| 25 | }; |
| 26 | |
| 27 | template <typename From> |
| 28 | struct to_impl<constant_tag, From, when<IntegralConstant<From>::value>> { |
| 29 | template <typename N> |
| 30 | static constexpr auto apply(N const&) |
| 31 | { return constant<N::value>{}; } |
| 32 | }; |
| 33 | }} |
| 34 | |
| 35 | // Make sure we really satisfy IntegralConstant<>. |
| 36 | static_assert(hana::IntegralConstant<constant<0>>::value, "" ); |
| 37 | static_assert(hana::IntegralConstant<constant<1>>::value, "" ); |
| 38 | static_assert(hana::IntegralConstant<constant<2>>::value, "" ); |
| 39 | |
| 40 | // Make sure we're also a model of Constant automatically. |
| 41 | static_assert(hana::Constant<constant<0>>::value, "" ); |
| 42 | static_assert(hana::Constant<constant<1>>::value, "" ); |
| 43 | static_assert(hana::Constant<constant<2>>::value, "" ); |
| 44 | |
| 45 | // Make sure we have the hana::value<> function defined automatically. |
| 46 | static_assert(hana::value<constant<0>>() == 0, "" ); |
| 47 | static_assert(hana::value<constant<1>>() == 1, "" ); |
| 48 | static_assert(hana::value<constant<2>>() == 2, "" ); |
| 49 | static_assert(hana::value<constant<3>>() == 3, "" ); |
| 50 | |
| 51 | |
| 52 | int main() { } |
| 53 | |