[SAoC] Move semantics and STL containers
Suleyman
sahmi.soulaimane at gmail.com
Wed Oct 23 17:19:14 UTC 2019
Week 5:
Start of milestone 2.
* Move constructor semantic implemented based on the work on
rvalue ref.
* A default move constructor is generated if one or more members
of a struct have move constructor.
* The move opAssign implemented as well.
* A `@move` attribute with an accompanying `__move()` intrinsic
was added as an alternative to rvalue ref.
Usage example:
1. with rvalue ref
compile with the compiler switch `-preview=rvalueattribute`.
```
struct S
{
static char[] result;
this(@rvalue ref inout S) inout { result ~= "Mc"; }
void opAssign(@rvalue ref inout S) inout { result ~= "Ma"; }
}
unittest
{
S a;
S b = cast(@rvalue ref)a;
a = cast(@rvalue ref)b;
assert(S.result == "McMa");
}
```
2. with the `@move` attribute
compile with the compiler switch `-preview=moveattribute`.
```
struct S
{
static char[] result;
this(ref inout S) @move inout { result ~= "Mc"; }
void opAssign(ref inout S) @move inout { result ~= "Ma"; }
}
unittest
{
S a;
S b = __move(a);
a = __move(b);
assert(S.result == "McMa");
}
```
Note: the implementation is not ready yet for things like
internal pointers, some interaction with the codegen is needed to
eliminate the remaining blits that still exist.
More information about the Digitalmars-d
mailing list