safety of move

Ellery Newcomer ellery-newcomer at utulsa.edu
Tue Nov 27 19:19:03 PST 2012


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. move works I guess because it uses memcpy 
or something. Not complaining, but wondering.

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



More information about the Digitalmars-d-learn mailing list