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.
This commit is contained in:
Thomas Spriggs 2019-01-18 16:35:23 +00:00
parent 4bd2317eb5
commit f8738c9943
2 changed files with 16 additions and 4 deletions

View File

@ -208,8 +208,8 @@ struct concat_iteratort
public:
using difference_type = typename first_iteratort::difference_type;
using value_type = typename first_iteratort::value_type;
using pointer = const value_type *;
using reference = const value_type &;
using pointer = typename first_iteratort::pointer;
using reference = typename first_iteratort::reference;
using iterator_category = std::forward_iterator_tag;
static_assert(
@ -245,14 +245,14 @@ public:
return tmp;
}
value_type &operator*()
reference operator*()
{
if(first_begin == first_end)
return *second_begin;
return *first_begin;
}
value_type *operator->()
pointer operator->()
{
if(first_begin == first_end)
return &(*second_begin);

View File

@ -76,6 +76,18 @@ SCENARIO("range tests", "[core][util][range]")
REQUIRE(odds == expected_odds);
}
}
GIVEN("Two const vectors of ints")
{
const std::vector<int> input1{1, 2};
const std::vector<int> input2{3, 4};
THEN("Concat the vectors using range.")
{
auto range = make_range(input1).concat(make_range(input2));
const std::vector<int> output{range.begin(), range.end()};
const std::vector<int> expected{1, 2, 3, 4};
REQUIRE(output == expected);
};
}
}
class move_onlyt