called copy constructor in foreach with ref on Range

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Jun 22 21:35:53 UTC 2020


On Monday, June 22, 2020 3:10:28 PM MDT kinke via Digitalmars-d-learn wrote:
> On Monday, 22 June 2020 at 20:51:37 UTC, Jonathan M Davis wrote:
> > [...]
>
> That's why I put the struct in parantheses. Moving a class ref
> makes hardly any sense, but I've also never written a *class* to
> represent a range. Moving is the no-brainer solution for
> transferring ownership of struct ranges and invalidating the
> original instance.

Invalidating the instance doesn't actually prevent it from being misused
though. At best, the fact that you moved the instance rather than copying it
makes it more likely that accidentally using the instance will cause more
extreme bugs or crash. The core issue that the original is potentially
invalid but can still be used exists whether you copy the range or move it.

Also, since the issue here is generic code, you have to take classes into
account and cannot assume that the range is a struct. True, it's usually a
bad idea to use classes for ranges, and ideally, we'd alter the range API so
that classes weren't valid ranges, but the reality of the matter is that
they are, and generic code has to take that into account.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list