• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/bool.hpp>
6 #include <boost/hana/fwd/hash.hpp>
7 #include <boost/hana/map.hpp>
8 #include <boost/hana/pair.hpp>
9 #include <boost/hana/type.hpp>
10 
11 #include <type_traits>
12 namespace hana = boost::hana;
13 
14 
15 struct NoDefault {
16     NoDefault() = delete;
17     NoDefault(NoDefault const&) = default;
NoDefaultNoDefault18     constexpr explicit NoDefault(int) { }
19 };
20 
operator ==(NoDefault const &,NoDefault const &)21 auto operator==(NoDefault const&, NoDefault const&) { return hana::true_c; }
operator !=(NoDefault const &,NoDefault const &)22 auto operator!=(NoDefault const&, NoDefault const&) { return hana::false_c; }
23 
24 // Note: It is also useful to check with a non-empty class, because that
25 //       triggers different instantiations due to EBO.
26 struct NoDefault_nonempty {
27     NoDefault_nonempty() = delete;
28     NoDefault_nonempty(NoDefault_nonempty const&) = default;
NoDefault_nonemptyNoDefault_nonempty29     constexpr explicit NoDefault_nonempty(int k) : i(k) { }
30     int i;
31 };
32 
operator ==(NoDefault_nonempty const &,NoDefault_nonempty const &)33 auto operator==(NoDefault_nonempty const&, NoDefault_nonempty const&) { return hana::true_c; }
operator !=(NoDefault_nonempty const &,NoDefault_nonempty const &)34 auto operator!=(NoDefault_nonempty const&, NoDefault_nonempty const&) { return hana::false_c; }
35 
36 struct Default {
37     Default() = default;
38     Default(Default const&) = default;
DefaultDefault39     constexpr explicit Default(int) { }
40 };
41 
operator ==(Default const &,Default const &)42 auto operator==(Default const&, Default const&) { return hana::true_c; }
operator !=(Default const &,Default const &)43 auto operator!=(Default const&, Default const&) { return hana::false_c; }
44 
45 namespace boost { namespace hana {
46     template <>
47     struct hash_impl<NoDefault> {
applyboost::hana::hash_impl48         static constexpr auto apply(NoDefault const&)
49         { return hana::type_c<NoDefault>; };
50     };
51 
52     template <>
53     struct hash_impl<NoDefault_nonempty> {
applyboost::hana::hash_impl54         static constexpr auto apply(NoDefault_nonempty const&)
55         { return hana::type_c<NoDefault_nonempty>; };
56     };
57 
58     template <>
59     struct hash_impl<Default> {
applyboost::hana::hash_impl60         static constexpr auto apply(Default const&)
61         { return hana::type_c<Default>; };
62     };
63 }}
64 
main()65 int main() {
66     auto map1 = hana::make_map(hana::make_pair(Default(1), Default(1)));
67     static_assert(std::is_default_constructible<decltype(map1)>::value, "");
68 
69     auto map2 = hana::make_map(hana::make_pair(NoDefault(1), NoDefault(1)));
70     static_assert(!std::is_default_constructible<decltype(map2)>::value, "");
71 
72     auto map3 = hana::make_map(hana::make_pair(NoDefault_nonempty(1), NoDefault_nonempty(1)));
73     static_assert(!std::is_default_constructible<decltype(map3)>::value, "");
74 }
75