D's equivalent to C++'s std::move?

Rikki Cattermole via Digitalmars-d digitalmars-d at puremagic.com
Mon Feb 1 05:28:02 PST 2016


On 02/02/16 2:21 AM, Shachar Shemesh wrote:
> Hi all,
>
> I have a non-copyable struct with move semantics. In other words, a
> struct with @disable this(this), but with working overloads for the
> this(copy) and opAssign.
>
> Now I have an instance of that struct. I would like to be able to
> voluntarily give up ownership for the sake of another instance.
>
> In C++, I would do something like this:
>
> unique_ptr<int> p (new int), q;
>
> q = std::move(p);
>
> I am unsure what is the correct way to do this under D.
>
> Shachar

So just to confirm, you want to explicitly copy a struct but not 
"implicitly" copy it?

struct Foo {
	@disable
	this(this);
	
	int x;
	
	Foo dup() {
		return Foo(x);	
	}
}

void main() {
	Foo a, b;
	a = Foo(7);
	b = a.dup;
	
	// will error
	// b = a;
}

Please note for this example code I have implemented dup, you wouldn't 
normally need to do that for structs.

Also D.learn is correct place to ask this. No the general N.G.


More information about the Digitalmars-d mailing list