1
+ // Copyright David Stone 2024.
2
+ // Distributed under the Boost Software License, Version 1.0.
3
+ // (See accompanying file LICENSE_1_0.txt or copy at
4
+ // http://www.boost.org/LICENSE_1_0.txt)
5
+
6
+ export module bounded.test.tombstone_traits;
7
+
8
+ import bounded.arithmetic.unary_minus;
9
+ import bounded.comparison;
10
+ import bounded.integer;
11
+ import bounded.literal;
12
+ import bounded.tombstone_traits;
13
+
14
+ namespace {
15
+ using namespace bounded ::literal;
16
+
17
+ struct a {
18
+ int m;
19
+ };
20
+
21
+ struct b {
22
+ constexpr b ():
23
+ m(1 )
24
+ {
25
+ }
26
+ a m;
27
+ private:
28
+ constexpr explicit b (bounded::tombstone_tag, auto const make):
29
+ m(make())
30
+ {
31
+ }
32
+ friend bounded::tombstone_traits_composer<&b::m>;
33
+ };
34
+
35
+ } // namespace
36
+
37
+ template <>
38
+ struct bounded ::tombstone_traits<a> {
39
+ static constexpr auto spare_representations = 1_bi;
40
+ static constexpr auto make (bounded::constant_t <0 >) noexcept -> a {
41
+ return a (0 );
42
+ }
43
+ static constexpr auto index (a const & value) noexcept -> bounded::integer<-1, 0> {
44
+ if (value.m == 0 ) {
45
+ return 0_bi;
46
+ }
47
+ return -1_bi;
48
+ }
49
+ };
50
+
51
+ template <>
52
+ struct bounded ::tombstone_traits<b> : bounded::tombstone_traits_composer<&b::m> {
53
+ };
54
+
55
+ namespace {
56
+
57
+ using a_traits = bounded::tombstone_traits<a>;
58
+ static_assert (a_traits::spare_representations == 1_bi);
59
+ static_assert (a_traits::make(0_bi).m == 0 );
60
+ static_assert (a_traits::index(a(0 )) == 0_bi);
61
+ static_assert (a_traits::index(a(1 )) == -1_bi);
62
+
63
+ using b_traits = bounded::tombstone_traits<b>;
64
+ static_assert (b_traits::spare_representations == 1_bi);
65
+ static_assert (b_traits::make(0_bi).m.m == 0 );
66
+ static_assert (b_traits::index(b_traits::make(0_bi)) == 0_bi);
67
+ static_assert (b_traits::index(b()) == -1_bi);
68
+
69
+ } // namespace
0 commit comments