summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/test/map/cnstr.default.cpp
blob: cd1320c8f4d70ccc955bf2170ca86bb68ff3c377 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 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/bool.hpp>
#include <boost/hana/fwd/hash.hpp>
#include <boost/hana/map.hpp>
#include <boost/hana/pair.hpp>
#include <boost/hana/type.hpp>

#include <type_traits>
namespace hana = boost::hana;


struct NoDefault {
    NoDefault() = delete;
    NoDefault(NoDefault const&) = default;
    constexpr explicit NoDefault(int) { }
};

auto operator==(NoDefault const&, NoDefault const&) { return hana::true_c; }
auto operator!=(NoDefault const&, NoDefault const&) { return hana::false_c; }

// Note: It is also useful to check with a non-empty class, because that
//       triggers different instantiations due to EBO.
struct NoDefault_nonempty {
    NoDefault_nonempty() = delete;
    NoDefault_nonempty(NoDefault_nonempty const&) = default;
    constexpr explicit NoDefault_nonempty(int k) : i(k) { }
    int i;
};

auto operator==(NoDefault_nonempty const&, NoDefault_nonempty const&) { return hana::true_c; }
auto operator!=(NoDefault_nonempty const&, NoDefault_nonempty const&) { return hana::false_c; }

struct Default {
    Default() = default;
    Default(Default const&) = default;
    constexpr explicit Default(int) { }
};

auto operator==(Default const&, Default const&) { return hana::true_c; }
auto operator!=(Default const&, Default const&) { return hana::false_c; }

namespace boost { namespace hana {
    template <>
    struct hash_impl<NoDefault> {
        static constexpr auto apply(NoDefault const&)
        { return hana::type_c<NoDefault>; };
    };

    template <>
    struct hash_impl<NoDefault_nonempty> {
        static constexpr auto apply(NoDefault_nonempty const&)
        { return hana::type_c<NoDefault_nonempty>; };
    };

    template <>
    struct hash_impl<Default> {
        static constexpr auto apply(Default const&)
        { return hana::type_c<Default>; };
    };
}}

int main() {
    auto map1 = hana::make_map(hana::make_pair(Default(1), Default(1)));
    static_assert(std::is_default_constructible<decltype(map1)>::value, "");

    auto map2 = hana::make_map(hana::make_pair(NoDefault(1), NoDefault(1)));
    static_assert(!std::is_default_constructible<decltype(map2)>::value, "");

    auto map3 = hana::make_map(hana::make_pair(NoDefault_nonempty(1), NoDefault_nonempty(1)));
    static_assert(!std::is_default_constructible<decltype(map3)>::value, "");
}