5
5
6
6
export module bounded.arithmetic.pointer;
7
7
8
- import bounded.arithmetic.plus;
9
8
import bounded.bounded_integer;
10
- import bounded.comparison;
11
- import bounded.integer;
12
-
13
- import numeric_traits;
14
- import std_module;
15
9
16
10
namespace bounded {
17
11
18
- namespace {
19
-
20
- // This is faster to compile than `std::is_pointer_v`
21
- template <typename >
22
- constexpr auto is_pointer = false ;
23
-
24
- template <typename T>
25
- constexpr auto is_pointer<T *> = true ;
26
-
27
- } // namespace
28
-
29
- export template <typename T, bounded_integer Integer> requires (
30
- (is_pointer<T> or (std::is_array_v<T> and numeric_traits::max_value<Integer> <= constant<std::extent_v<T>>))
31
- )
32
- constexpr auto operator+(T const & array, Integer const number) {
33
- return array + number.value ();
12
+ export template <typename T>
13
+ constexpr auto operator +(T * const & ptr, bounded_integer auto const number) -> T * {
14
+ return ptr + number.value ();
34
15
}
35
16
36
17
37
- export template <bounded_integer Integer, typename T> requires (
38
- (is_pointer<T> or (std::is_array_v<T> and numeric_traits::max_value<Integer> <= constant<std::extent_v<T>>))
39
- )
40
- constexpr auto operator+(Integer const number, T const & array) {
41
- return number.value () + array;
18
+ export template <typename T>
19
+ constexpr auto operator +(bounded_integer auto const number, T * const & ptr) -> T * {
20
+ return number.value () + ptr;
42
21
}
43
22
44
- // Not possible to overload operator[]. See
45
- // https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/CmBERU_sr8Y
46
-
47
- } // namespace bounded
48
-
49
- namespace {
50
-
51
- using array_type = int [5 ];
52
- constexpr array_type array{ 0 , 1 , 2 , 3 , 4 };
53
-
54
- static_assert (
55
- *(std::begin(array) + bounded::constant<0 >) == 0 ,
56
- " Incorrect pointer arithmetic with bounded::integer."
57
- );
58
- static_assert (
59
- *(array + bounded::constant<0 >) == 0 ,
60
- " Incorrect array arithmetic with bounded::integer."
61
- );
62
- static_assert (
63
- array + bounded::constant<5 > == std::end(array),
64
- " Incorrect array arithmetic with bounded::integer."
65
- );
66
-
67
-
68
- #if 0
69
- // TODO: Test that this does not compile, improve error message
70
- array + bounded::constant<6>
71
- #endif
72
-
73
- // Oops, not possible to overload array index operator
74
- #if 0
75
- static_assert(
76
- array[bounded::constant<0>] == 0,
77
- "Incorrect array indexing with bounded::integer."
78
- );
79
- #endif
80
-
81
- } // namespace
23
+ } // namespace bounded
0 commit comments