Fixed move_iterator::operator+= and operator-= return type

move_iterator::operator+= / operator-= returned by value, but RandomAccessIterator
requirements (and std::move_iterator) expect these to return move_iterator&.
Returning by value also added an unnecessary copy and could break generic code
expecting reference semantics.
This commit is contained in:
Roland Reichwein 2026-05-31 14:02:32 +02:00
parent 288587070c
commit 8b0d8888e6
2 changed files with 24 additions and 2 deletions

View File

@ -576,13 +576,13 @@ namespace etl
return move_iterator(current - n);
}
ETL_CONSTEXPR14 move_iterator operator+=(difference_type n)
ETL_CONSTEXPR14 move_iterator& operator+=(difference_type n)
{
current += n;
return *this;
}
ETL_CONSTEXPR14 move_iterator operator-=(difference_type n)
ETL_CONSTEXPR14 move_iterator& operator-=(difference_type n)
{
current -= n;
return *this;

View File

@ -550,6 +550,28 @@ namespace
CHECK_EQUAL(true, mitr->valid);
}
//*************************************************************************
TEST(test_move_iterator_compound_assignment_returns_reference)
{
int data[] = {10, 20, 30, 40, 50};
etl::move_iterator<int*> mitr(&data[0]);
// operator+= must return a reference to *this
etl::move_iterator<int*>& ref_plus = (mitr += 2);
CHECK_EQUAL(&mitr, &ref_plus);
CHECK_EQUAL(30, *mitr);
// operator-= must return a reference to *this
etl::move_iterator<int*>& ref_minus = (mitr -= 1);
CHECK_EQUAL(&mitr, &ref_minus);
CHECK_EQUAL(20, *mitr);
// Chaining should work
(mitr += 1) += 1;
CHECK_EQUAL(40, *mitr);
}
//*************************************************************************
TEST(test_move_iterator_subtraction)
{