Skip to content

Commit 6060a87

Browse files
committed
Add const to begin() accessor of ranget
It was previously impossible to iterate over a `const` range, because you couldn't call `.begin()` on it. This fixes that issue. Changing this to `const` does not allow the internals of the range to be mutated, as it returns a copy of the begin iterator, not a reference to it. This commit adds `const` to ranges in range unit test to show that ranges can be made const correct, using this fix.
1 parent f8738c9 commit 6060a87

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/util/range.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ struct ranget final
365365
return begin_value == end_value;
366366
}
367367

368-
iteratort begin()
368+
iteratort begin() const
369369
{
370370
return begin_value;
371371
}

unit/util/range.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ SCENARIO("range tests", "[core][util][range]")
2121
list.emplace_back("acdef");
2222
THEN("Use range-for to compute the total length")
2323
{
24-
auto range = make_range(list);
24+
const auto range = make_range(list);
2525
std::size_t total_length = 0;
2626
for(const auto &s : range)
2727
total_length += s.length();
2828
REQUIRE(total_length == 12);
2929
}
3030
THEN("Use map to compute individual lengths")
3131
{
32-
auto length_range =
32+
const auto length_range =
3333
make_range(list).map([](const std::string &s) { return s.length(); });
3434
auto it = length_range.begin();
3535
REQUIRE(*it == 3);
@@ -42,7 +42,7 @@ SCENARIO("range tests", "[core][util][range]")
4242
}
4343
THEN("Filter using lengths")
4444
{
45-
auto filtered_range = make_range(list).filter(
45+
const auto filtered_range = make_range(list).filter(
4646
[&](const std::string &s) { return s.length() == 4; });
4747
auto it = filtered_range.begin();
4848
REQUIRE(*it == "cdef");
@@ -51,7 +51,7 @@ SCENARIO("range tests", "[core][util][range]")
5151
}
5252
THEN("Filter, map and use range-for on the same list")
5353
{
54-
auto range =
54+
const auto range =
5555
make_range(list)
5656
.filter([&](const std::string &s) -> bool { return s[0] == 'a'; })
5757
.map([&](const std::string &s) { return s.length(); });
@@ -69,7 +69,7 @@ SCENARIO("range tests", "[core][util][range]")
6969
const std::vector<int> input{1, 2, 3, 4};
7070
THEN("Filter the vector using range.")
7171
{
72-
auto odds_range =
72+
const auto odds_range =
7373
make_range(input).filter([](const int number) { return number % 2; });
7474
const std::vector<int> odds{odds_range.begin(), odds_range.end()};
7575
const std::vector<int> expected_odds{1, 3};
@@ -82,7 +82,7 @@ SCENARIO("range tests", "[core][util][range]")
8282
const std::vector<int> input2{3, 4};
8383
THEN("Concat the vectors using range.")
8484
{
85-
auto range = make_range(input1).concat(make_range(input2));
85+
const auto range = make_range(input1).concat(make_range(input2));
8686
const std::vector<int> output{range.begin(), range.end()};
8787
const std::vector<int> expected{1, 2, 3, 4};
8888
REQUIRE(output == expected);
@@ -122,13 +122,13 @@ SCENARIO(
122122
input.emplace_back(i);
123123
THEN("Values from a range of made from the vector can be moved.")
124124
{
125-
auto input_range = make_range(input);
125+
const auto input_range = make_range(input);
126126
move_onlyt destination{std::move(*input_range.begin())};
127127
REQUIRE(destination.value == 1);
128128
}
129129
THEN("A range of made from the vector can be filtered.")
130130
{
131-
auto odds_filter = make_range(input).filter(is_odd);
131+
const auto odds_filter = make_range(input).filter(is_odd);
132132
const std::size_t total =
133133
std::distance(odds_filter.begin(), odds_filter.end());
134134
REQUIRE(total == 5);

0 commit comments

Comments
 (0)