Skip to content

Commit 431989c

Browse files
committed
Do not support array + integer or integer + array. Require that the argument is a pointer instead.
1 parent f019165 commit 431989c

File tree

4 files changed

+52
-65
lines changed

4 files changed

+52
-65
lines changed

source/bounded/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ set_source_files_properties(arithmetic/operators_builtin.cpp
117117
PROPERTIES COMPILE_FLAGS "-Wno-implicit-int-conversion -Wno-shorten-64-to-32"
118118
)
119119

120+
add_subdirectory(arithmetic/test)
121+
120122
add_executable(bounded_test)
121123

122124
target_sources(bounded_test PRIVATE

source/bounded/arithmetic/pointer.cpp

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,19 @@
55

66
export module bounded.arithmetic.pointer;
77

8-
import bounded.arithmetic.plus;
98
import bounded.bounded_integer;
10-
import bounded.comparison;
11-
import bounded.integer;
12-
13-
import numeric_traits;
14-
import std_module;
159

1610
namespace bounded {
1711

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();
3415
}
3516

3617

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;
4221
}
4322

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
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
add_library(bounded_arithmetic_test)
7+
8+
target_sources(bounded_arithmetic_test PRIVATE
9+
FILE_SET CXX_MODULES
10+
BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}"
11+
FILES
12+
pointer.cpp
13+
)
14+
15+
target_link_libraries(bounded_arithmetic_test PUBLIC
16+
bounded
17+
strict_defaults
18+
)
19+
20+
add_test(bounded_arithmetic_test bounded_arithmetic_test)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.arithmetic.test.pointer;
7+
8+
import bounded.arithmetic.pointer;
9+
10+
import bounded.bounded_integer;
11+
import bounded.literal;
12+
13+
import std_module;
14+
15+
namespace {
16+
using namespace bounded::literal;
17+
18+
constexpr int array[5] = {};
19+
20+
static_assert(std::begin(array) + 0_bi == std::begin(array));
21+
static_assert(std::begin(array) + 5_bi == std::end(array));
22+
23+
} // namespace

0 commit comments

Comments
 (0)