summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/test/assert/constant.cpp
blob: 3eca58c180260174c02396d4b4f1895a916535b1 (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
// 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 <support/cnumeric.hpp>
namespace hana = boost::hana;


template <bool value>
auto constant_bool() { return make_cnumeric<bool, value>(); }

template <bool value>
constexpr auto constexpr_constant_bool() { return make_cnumeric<bool, value>(); }


// Make sure it works at global scope
BOOST_HANA_CONSTANT_ASSERT(constant_bool<true>());
BOOST_HANA_CONSTANT_ASSERT_MSG(constant_bool<true>(), "message");

// Make sure it works at namespace scope
namespace ns {
    BOOST_HANA_CONSTANT_ASSERT(constant_bool<true>());
    BOOST_HANA_CONSTANT_ASSERT_MSG(constant_bool<true>(), "message");
}

// Make sure it works in a constexpr context
constexpr bool constexpr_context() {
    BOOST_HANA_CONSTANT_ASSERT(constexpr_constant_bool<true>());
    BOOST_HANA_CONSTANT_ASSERT_MSG(constexpr_constant_bool<true>(), "message");
    return true;
}
static_assert(constexpr_context(), "");


int main() {
    // Make sure it works at function scope
    BOOST_HANA_CONSTANT_ASSERT(constant_bool<true>());
    BOOST_HANA_CONSTANT_ASSERT_MSG(constant_bool<true>(), "message");

    // Make sure it works inside a lambda
    auto lambda = []{
        BOOST_HANA_CONSTANT_ASSERT(constant_bool<true>());
        BOOST_HANA_CONSTANT_ASSERT_MSG(constant_bool<true>(), "message");
    };
    lambda();

    // Make sure we can reference a local variable
    auto yes = constant_bool<true>();
    BOOST_HANA_CONSTANT_ASSERT(yes);
    BOOST_HANA_CONSTANT_ASSERT_MSG(yes, "message");
}