Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions std/algorithm.d
Original file line number Diff line number Diff line change
Expand Up @@ -1455,10 +1455,15 @@ $(D &source == &target || !pointsTo(source, source))
*/
void move(T)(ref T source, ref T target)
{
assert(!pointsTo(source, source));
if (__ctfe)
{
target = source;
return;
}
static if (is(T == struct))
{
if (&source == &target) return;
assert(!pointsTo(source, source));
if (&source == &target) return;
// Most complicated case. Destroy whatever target had in it
// and bitblast source over it
static if (hasElaborateDestructor!T) typeid(T).destroy(&target);
Expand Down Expand Up @@ -1543,14 +1548,28 @@ unittest
move(s41, s42);
assert(s41.x.n == 0);
assert(s42.x.n == 1);

static assert((){
S1 x, y;
x.a = 123;
move(x, y);
return y;
}().a == 123);
}

/// Ditto
T move(T)(ref T source)
// Fake a move via copy
// TODO: need a better way to actually move stuff at CTFE
T ctfeMove(T)(ref T source)
{
// Can avoid to check aliasing.
return source;
}

T realMove(T)(ref T source)
{
// Can avoid to check aliasing.

T result = void;

static if (is(T == struct))
{
// Can avoid destructing result.
Expand Down Expand Up @@ -1583,6 +1602,12 @@ T move(T)(ref T source)
return result;
}

/// Ditto
T move(T)(ref T source)
{
return __ctfe ? ctfeMove(source) : realMove(source);
}

unittest
{
debug(std_algorithm) scope(success)
Expand Down Expand Up @@ -1628,6 +1653,12 @@ unittest
S4 s42 = move(s41);
assert(s41.x.n == 0);
assert(s42.x.n == 1);

static assert((){
S1 s = S1(22, 33);
auto s2 = move(s);
return s2;
}().b == 33);
}

unittest//Issue 6217
Expand Down