Re: [PATCH 1/3] gdb/objfiles: make objfile::sections yield references

Simon Marchi
2025-08-29T15:48:02Z

On 2025-08-29 09:34, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
>
> Simon> A patch later in this series would change objfile::section_iterator to
> Simon> yield `obj_section &` instead of `obj_section *`. Do it as a
> Simon> preparatory patch to avoid polluting that subsequent patch. I think it
> Simon> would make sense on its own anyway.
>
> Seems ok.
>
> Simon> - typedef section_iterator self_type;
> Simon> - typedef obj_section *value_type;
> Simon> + using self_type = section_iterator;
> Simon> + using reference = obj_section &;
>
> I never remember when value_type or reference need to be declared.
> Is it really correct to remove value_type?

I don't know for sure, but cppreference's iterator_traits page [1] says
this for C++17:

Nested type Definition
----------- ----------
difference_type Iter::difference_type
value_type Iter::value_type
pointer Iter::pointer
reference Iter::reference
iterator_category Iter::iterator_category

If Iter does not have any of the five nested types above, then this
template has no members by any of those names (std::iterator_traits
is SFINAE-friendly). (valid for C++17, until C++20)

So I guess if you tried to use iterator_traits with a type missing any
of those typedefs, it wouldn't work. But the original section_iterator
already missing reference and som others, so it's already in that state.
If that ever becomes a problem, I think we'll get a build failure, so
we'll know. I think I had some cases of that in an earlier iteration of
this series, where I had to add the missing member types for the
compiler to be happy.

And anyway, this particular code disappears in the last patch of the
series, so I'm not too worried about that particular instance.

[1] https://en.cppreference.com/w/cpp/iterator/iterator_traits.html

>
> Approved-By: Tom Tromey <tom@tromey.com>

Thanks, I will push this one right away.

Simon