Skip to content

Commit f8738c9

Browse files
committed
Fix constness on dereferencing a concat_iteratort
Dereferencing a `concat_iteratort` from concating const collections with `ranget` would previously introduce a compile error, due to a `const` to non-const conversion. This commit fixes this, by using the same return type as the iterator which it wraps around. This commit includes a test for using range to concat `const` collections.
1 parent 4bd2317 commit f8738c9

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/util/range.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ struct concat_iteratort
208208
public:
209209
using difference_type = typename first_iteratort::difference_type;
210210
using value_type = typename first_iteratort::value_type;
211-
using pointer = const value_type *;
212-
using reference = const value_type &;
211+
using pointer = typename first_iteratort::pointer;
212+
using reference = typename first_iteratort::reference;
213213
using iterator_category = std::forward_iterator_tag;
214214

215215
static_assert(
@@ -245,14 +245,14 @@ struct concat_iteratort
245245
return tmp;
246246
}
247247

248-
value_type &operator*()
248+
reference operator*()
249249
{
250250
if(first_begin == first_end)
251251
return *second_begin;
252252
return *first_begin;
253253
}
254254

255-
value_type *operator->()
255+
pointer operator->()
256256
{
257257
if(first_begin == first_end)
258258
return &(*second_begin);

unit/util/range.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ SCENARIO("range tests", "[core][util][range]")
7676
REQUIRE(odds == expected_odds);
7777
}
7878
}
79+
GIVEN("Two const vectors of ints")
80+
{
81+
const std::vector<int> input1{1, 2};
82+
const std::vector<int> input2{3, 4};
83+
THEN("Concat the vectors using range.")
84+
{
85+
auto range = make_range(input1).concat(make_range(input2));
86+
const std::vector<int> output{range.begin(), range.end()};
87+
const std::vector<int> expected{1, 2, 3, 4};
88+
REQUIRE(output == expected);
89+
};
90+
}
7991
}
8092

8193
class move_onlyt

0 commit comments

Comments
 (0)