From: Tom de Vries <tdevries@suse.de>
To: Tom Tromey <tom@tromey.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 3/7] [gdbsupport] Factor out base_next_iterator
Date: Fri, 1 May 2026 14:54:10 +0200 [thread overview]
Message-ID: <6201f741-6cd9-4d37-ab6b-9c888f4069d2@suse.de> (raw)
In-Reply-To: <91f3f93c-e4c9-410d-a5b6-93d0cdfd2414@suse.de>
On 4/30/26 6:49 AM, Tom de Vries wrote:
> On 4/24/26 6:21 PM, Tom Tromey wrote:
>> Tom> + Instead of factoring out a base class, we could use something
>> like this:
>> Tom> +
>> Tom> + template<typename T, auto F = &T::next>
>> Tom> + struct next_iterator
>> Tom> + {
>> Tom> + ...
>> Tom> + self_type &operator++ ()
>> Tom> + {
>> Tom> + m_item = m_item->*F;
>> Tom> + return *this;
>> Tom> + }
>> Tom> + ...
>> Tom> + }
>> Tom> +
>> Tom> + but that has the drawback that it doesn't work with incomplete
>> T. */
>> I am curious about this because I wonder how the current code could work
>> with an incomplete T -- since the code references T::next directly.
>
> Yeah, that's a good question.
>
> I wrote the following stand-alone demonstrator:
> ...
> $ cat test.c
> #include <cstddef>
> #include <utility>
> #include <iterator>
> #include <cstdio>
>
> #include "gdbsupport/next-iterator.h"
>
> struct list;
>
> void
> print_pointer (list *l)
> {
> auto begin = next_iterator<struct list> (l);
> for (auto elem : next_range<struct list> (begin))
> printf ("%p\n", elem);
> }
>
> struct list
> {
> int i;
> struct list *next;
> };
>
> void
> print_int (list *l)
> {
> auto begin = next_iterator<struct list> (l);
> for (auto elem : next_range<struct list> (begin))
> printf ("%d\n", elem->i);
> }
>
> struct list a = { 1, nullptr };
> struct list b = { 2, &a };
> struct list c = { 3, &b };
>
> int
> main ()
> {
> print_pointer (&c);
> print_int (&c);
>
> return 0;
> }
> $ g++ test.c -I .
> $ ./a.out
> 0x404040
> 0x404030
> 0x404020
> 3
> 2
> 1
> $
> ...
>
> I asked an LLM about this, and it explained that this is deferred or
> delayed template instantiation at work.
>
> Basically the operator++ is used in print_pointer, but not yet
> instantiated, so we don't have to know the details of the incomplete
> type at that point.
I've written a next_iterator unit test, and added (part of) this
demonstrator (
https://sourceware.org/pipermail/gdb-patches/2026-May/227065.html ).
Thanks,
- Tom
next prev parent reply other threads:[~2026-05-01 12:54 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-23 6:35 [PATCH 0/7] [gdb] Add superblocks range loops Tom de Vries
2026-04-23 6:35 ` [PATCH 1/7] [gdb] Use block::function_block Tom de Vries
2026-04-23 14:33 ` Tom Tromey
2026-04-24 12:19 ` Tom de Vries
2026-04-23 6:35 ` [PATCH 2/7] [gdbsupport] Add parameterless iterator_range constructor Tom de Vries
2026-04-23 14:34 ` Tom Tromey
2026-04-24 12:20 ` Tom de Vries
2026-04-23 6:35 ` [PATCH 3/7] [gdbsupport] Factor out base_next_iterator Tom de Vries
2026-04-30 16:24 ` Simon Marchi
2026-04-30 19:09 ` Simon Marchi
2026-05-01 12:57 ` Tom Tromey
2026-05-01 13:20 ` Tom de Vries
2026-05-01 13:15 ` Tom de Vries
[not found] ` <87340kpbwx.fsf@tromey.com>
2026-04-30 4:49 ` Tom de Vries
2026-05-01 12:54 ` Tom de Vries [this message]
2026-05-01 13:00 ` Tom de Vries
2026-04-23 6:35 ` [PATCH 4/7] [gdb] Add block::superblocks Tom de Vries
2026-04-27 11:11 ` Jan Vrany
2026-05-01 13:06 ` Tom de Vries
2026-04-30 16:24 ` Simon Marchi
2026-05-01 13:19 ` Tom de Vries
2026-04-23 6:35 ` [PATCH 5/7] [gdb] Use block::super_blocks Tom de Vries
2026-04-24 16:27 ` Tom Tromey
2026-04-24 21:24 ` Tom de Vries
2026-04-23 6:35 ` [PATCH 6/7] [gdb] Add block::function_blocks Tom de Vries
2026-04-23 6:35 ` [PATCH 7/7] [gdb] Use block::function_blocks Tom de Vries
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6201f741-6cd9-4d37-ab6b-9c888f4069d2@suse.de \
--to=tdevries@suse.de \
--cc=gdb-patches@sourceware.org \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox