Re: [PATCH v1 04/10] gdb support: add gdb::replace algorithm for iterators and ranges

Luis <[email protected]> Tue, 21 Jul 2026 22:33:04 +0100
Newsgroups gmane.comp.gdb.patches
Message-ID <[email protected]>
Drive-by review, and C++20 issues aside.

On 07/07/2026 16:48, Matthieu Longo wrote:
> Add a gdb::replace helper that mirrors the behavior of std::replace
> for iterator pairs, together with a convenience overload accepting a
> range.
> 
> This provides a C++17-compatible replacement for the C++20 std::replace/
> std::ranges::replace algorithms, allowing callers to use a consistent
> interface until GDB transitions to C++20. The helpers should be removed
> once the C++ standard library implementations become available.
> ---
>   gdbsupport/array-view.h | 26 ++++++++++++++++++++++++++
>   1 file changed, 26 insertions(+)
> 
> diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h
> index 8431d7f5add..61119d7a8e2 100644
> --- a/gdbsupport/array-view.h
> +++ b/gdbsupport/array-view.h
> @@ -225,6 +225,32 @@ void copy (gdb::array_view<U> src, gdb::array_view<T> dest)
>       std::copy_backward (src.begin (), src.end (), dest.end ());
>   }
>   
> +/* Replace all occurrences of a value in the provided range.
> +
> +   Note: this helper is a reimplementation of std::replace, only available
> +   from C++20 onwards, and consequently, should be removed once we switch
> +   to C++20.  */
> +
> +template <class ForwardIt, typename T>
> +void replace (ForwardIt first, ForwardIt last,
> +	      const T &old_value, const T &new_value)
> +{
> +  for (auto it = first; it != last; ++it)
> +  {
> +    if (*it == old_value)
> +      *it = new_value;
> +  }

Formatting: Identation of the braces is off.

> +}
> +
> +/* Replace all occurrences of a value in the provided array view.
> +   Note: from C++20 onwards, std::ranges::replace should be used instead.  */
> +
> +template <class Range, typename T>
> +void replace (Range r, const T &old_value, const T &new_value)
> +{
> +  replace (r.begin (), r.end (), old_value, new_value);
> +}
> +
>   /* Compare LHS and RHS for (deep) equality.  That is, whether LHS and
>      RHS have the same sizes, and whether each pair of elements of LHS
>      and RHS at the same position compares equal.  */