Move Semantics
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Sep 29 11:22:36 PDT 2015
On Tuesday 29 September 2015 16:38, Alex wrote:
> Another question on move semantics from the cheap seats...
>
> See my code here: http://dpaste.dzfl.pl/995c5af59dd6
[...]
> The first (minor) question is:
> I have to initialize some dummy inner objects, before I can apply
> the move action. Is this really necessary? It won't be that
> problem I think, if it is so, but it would be nicer, if I could
> just perform the move operation.
Accessing an non-existing element of an associative array doesn't initialize
it. You have to assign to it. I've been slightly annoyed by this, too. I'm
not sure what the reasons for the current behavior are. I guess it would
slow down accesses (a bit? a lot?).
> The second question is:
> Following my code, the inner object I moved does not disappear
> from the array in the source outer object. Why? I tried it
> without, with an empty and with a non empty destructor, the
> result was the same.
`move` doesn't know what greater structure you're moving from. It just takes
two locations. There's no way for it to figure out that the source location
is an element of an associative array or whatever.
> And the third, main, question is:
> After I moved the inner object to the new outer object, the
> pointer to the outer object remains in the old state, to the
> source outer object. This is not what I expected! Well, yes this
> is some subjective expectation, but shouldn't an implicit pointer
> update itself to not break the logic of what it points to?
Your view on the relations of the objects is that the Inner objects in an
Outer's _innerarr field are owned by that Outer object. The compiler and
`move` have no such notion. Objects of nested classes are not restricted to
exist in fields of their .outer objects.
All in all, I think you expected more from nested classes and `move` than
they provide.
An object of a (non-static) nested class just has a pointer to an object of
the outer class. All this allows is some shorter syntax. The bond between
the two objects isn't any tighter than other pointers.
`move` does little more than copying. It avoids postblits and destroys the
source if necessary (i.e. reset to .init). It doesn't have any notion of
ownership transfer.
More information about the Digitalmars-d-learn
mailing list