Move Constructor Syntax
ShadoLight
ettienne.gilbert at gmail.com
Wed Oct 16 15:28:27 UTC 2024
On Wednesday, 16 October 2024 at 15:02:46 UTC, ShadoLight wrote:
> On Wednesday, 16 October 2024 at 13:23:53 UTC, Arafel wrote:
>> On 16.10.24 15:09, ShadoLight wrote:
>>>> That would make sense, but this would in turn mean that the
>>>> move constructor can never be invoked explicitly.
>>>
...
>
> Something like:
> ```d
> S a, b, c;
> a = S(1); // case(1) -> this (int i)
> b = S(a); // case(2) -> this (ref S s) -> implicit
> copy ctor
> c = S(S(2)); // case(3) -> this (S s)
> // ...and a is valid here
> ```
>
I know that `c = S(S(2));` is a bit of an silly rvalue example
(just do `c = S(2);` for the same result), but it is just to show
the principle. For a somewhat better example:
```d
struct S {
this (int i) { } //ctor
this (S s) { } //move ctor
//implicitly generated copy ctor if needed
}
struct X {
static int j;
static S factoryS() {
S s = S(j++);
return s;
}
}
void main() {
S a, b, c;
a = S(1); // ctor
b = S(a); // (implicit) copy ctor
c = S(X.factoryS()); // move ctor
}
```
More information about the Digitalmars-d
mailing list