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>
10namespace hana = boost::hana;
11
12
13// Define a simple model of IntegralConstant
14struct constant_tag { using value_type = int; };
15template <int i>
16struct constant {
17 static constexpr int value = i;
18 using hana_tag = constant_tag;
19};
20
21namespace 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<>.
36static_assert(hana::IntegralConstant<constant<0>>::value, "");
37static_assert(hana::IntegralConstant<constant<1>>::value, "");
38static_assert(hana::IntegralConstant<constant<2>>::value, "");
39
40// Make sure we're also a model of Constant automatically.
41static_assert(hana::Constant<constant<0>>::value, "");
42static_assert(hana::Constant<constant<1>>::value, "");
43static_assert(hana::Constant<constant<2>>::value, "");
44
45// Make sure we have the hana::value<> function defined automatically.
46static_assert(hana::value<constant<0>>() == 0, "");
47static_assert(hana::value<constant<1>>() == 1, "");
48static_assert(hana::value<constant<2>>() == 2, "");
49static_assert(hana::value<constant<3>>() == 3, "");
50
51
52int main() { }
53

source code of boost/libs/hana/test/concept/integral_constant.cpp