From 8b0d8888e60703dd3729e750b700e889b45fbe49 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 31 May 2026 14:02:32 +0200 Subject: [PATCH] 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. --- include/etl/iterator.h | 4 ++-- test/test_iterator.cpp | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/include/etl/iterator.h b/include/etl/iterator.h index b63c38c9..a990fb6c 100644 --- a/include/etl/iterator.h +++ b/include/etl/iterator.h @@ -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; diff --git a/test/test_iterator.cpp b/test/test_iterator.cpp index fac7f09e..db018c40 100644 --- a/test/test_iterator.cpp +++ b/test/test_iterator.cpp @@ -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 mitr(&data[0]); + + // operator+= must return a reference to *this + etl::move_iterator& ref_plus = (mitr += 2); + CHECK_EQUAL(&mitr, &ref_plus); + CHECK_EQUAL(30, *mitr); + + // operator-= must return a reference to *this + etl::move_iterator& 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) {