[Issue 16455] Wrong code when calling a struct delegate
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Thu Sep 1 11:54:57 PDT 2016
https://issues.dlang.org/show_bug.cgi?id=16455
--- Comment #4 from ag0aep6g at gmail.com ---
(In reply to apham from comment #2)
> Based on this https://dlang.org/spec/function.html#closures and the code,
> the struct var is not moving anywhere and not out of scope, so it must work
The NodeRange struct may be moved during construction (constructed at one
location, then moved to the target location). If this happens, it invalidates
the internal pointer you set up in the constructor. Returning from `children`
may mean another copy/move.
There are no hard rules here. The compiler has some leeway. From
<https://dlang.org/spec/struct.html> (second paragraph):
> A struct is defined to not have an identity; that is, the implementation is free to make bit copies of the struct as convenient.
(In reply to apham from comment #3)
> need to move the setting "doPopFront = &doMove;" out of constructor to make
> it work. Can move it to empty() to complete the initialization
Note that any copy/move of the struct will still invalidate the pointer. And as
far as I understand, the compiler may assume that the struct does not point to
itself. So you may run into trouble, even when you have no explicit copy/move
in your code.
--
More information about the Digitalmars-d-bugs
mailing list