https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112307

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
  return EnumeratorRange(Enumerator(std::views::single(Intersection())));

This creates a temporary Intersection object, then copies that into a
single_view object. Then that is copied into an Enumerator object which looks
like:

Enumerator {
  single_view<Intersection> range_;
  optional<single_view<Intersection>::_Iterator> begin_;
};

The range_ member is the copy of std::views::single(Intersection()) and the
begin_ member is initially empty.

Then that Enumerator is copied into an EnumeratorRange which calls Next() on
the new copy, which sets its begin_ member to point to the range_ member. Then
the EnumeratorRange is returned.

So I think it's expected that the Enumerator points to itself, because of the
call to its Next() member.

BUT, the self-referential pointer is set to the address of the range_ member
before the return value is copied, and so goes out of scope when that object is
copied via registers and then copied again into the automatic variable in
main().

So yes, I think it's invalid for the same reason as PR 109945 comment 25
explains, and indeed giving any of the objects a non-trivial destructor
prevents it being copied via registers and so the pointer isn't invalidated.

Reply via email to