Re: [PATCH 1/3] gdbsupport: add gdb::enumerate util

Simon Marchi
2026-02-21T00:59:00Z

On 2026-02-20 15:47, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
>
> Simon> From: Simon Marchi <simon.marchi@polymtl.ca>
> Simon> Add a "enumerate" utility, that acts pretty much like Python's
> Simon> enumerate(). It makes the code slightly nicer, for when you would
> Simon> otherwise need to handle the counter by hand.
>
> Very handy.
>
> Simon> +namespace gdb
> Simon> +{
> Simon> +
>
> I suggest adding more namespaces and calling this
> gdb::ranges::views::enumerate. Then when gdb uses C++23, we can perhaps
> just s/gdb/std/ and not reformat everything.
>
> https://en.cppreference.com/w/cpp/ranges/enumerate_view.html
>
> ... in particular note (2)
>
> I guess we could go the other way but I tend to think it's better in the
> end to just use the standard names and not supply renamings of things.

I don't have a strong opinion about this, but I don't mind doing as you
propose. It's just unfortunate that `gdb::ranges::views::enumerate` is
a bit verbose for nothing, but if this is what we'll have to use when we
get to C++23, then so be it. Perhaps we will have increased the 80
character limit by then, who knows.

> Simon> +template<typename T>
> Simon> +struct enumerated_element
> Simon> +{
> Simon> + std::size_t index;
> Simon> + T value;
> Simon> +};
>
> C++23 uses a tuple here.
>
> It also uses a reference but I am not sure if that matters for us.

In my version it's also a reference because of:

using value_type
= enumerated_element<typename std::iterator_traits<base_iterator>
::reference>;

We want it to be a reference in most cases, to avoid copying the value.

I can try to use a tuple. It will probably not make a big difference,
because I expect all uses to use structured bindings:

for ([i, v] : ...)

Thanks,

Simon