safety of move

Dmitry Olshansky dmitry.olsh at gmail.com
Wed Nov 28 07:44:27 PST 2012


11/28/2012 7:19 AM, Ellery Newcomer пишет:
> I find myself using [abusing?] move lately:
>
> import std.algorithm;
> import std.stdio;
>
> struct A {
>      const(int) i;
>      int j;
>      int k;
> }
>
> void main() {
>      A* a = new A(); // pretend this is malloc or something
>      // *a = A(1)
>      A a2 = A(1);
>      move(a2, *a);
>
>      A[] arr = new A[](2);
>      //arr[1] = *a;
>      move(*a, arr[1]);
> }
>
> For the first part, I have a A* pointing to uninitialized memory and I
> need to initialize it somehow.

emplace should work for constructing A in a given chunk of memory.

> move works I guess because it uses memcpy
> or something. Not complaining, but wondering.
>

Yes it hacks through const/immutable at ease. The only requirement seems 
that it has to be shallow immutable/cont.

> The second part violates D's const semantics and maybe shouldn't be
> permitted. But it is.

I agree.

-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list