Move Constructor Syntax
Arafel
er.krali at gmail.com
Tue Oct 15 19:29:11 UTC 2024
On 15/10/24 20:29, Max Samukha wrote:
> On Tuesday, 15 October 2024 at 17:52:43 UTC, Arafel wrote:
>
>> a move constructor invalidates the source.
>
> Only if the source is not used after the construction/assignment.
> Otherwise, a copy is made. I'd recommend to skim through DIP1040 if only
> for fun.
>
I had a look at it before posting, and according to it [1] (my bold):
> A Move Constructor for struct S is declared as:
>
> ```d
> this(S s) { ... }
> ```
>
> [...]
>
> A _Move Constructor_ is a struct member constructor that moves,
rather than copies, the argument corresponding to its first parameter
into the object to be constructed. **The argument is invalid after this
move**, and is not destructed.
Also, the examples I could see were about _implicit_ calls to the move
constructor ([2]). I found no example of explicit calls to them, nor a
description of what to expect in that case, but I might have missed it.
In any case, it would be then helpful to clarify it more prominently:
what happens if a constructor with the signature of a move constructor
is called explicitly like this?
```d
struct S {
this (int i) { }
this (S s) { }
}
void main() {
S s1, s2;
s1 = S(1);
s2 = S(s1);
// Is s1 valid here?
}
```
Because this is currently valid D code, even if Walter thinks it
shouldn't (as per the bug referenced in DIP1040 itself [3]), and s1 is
perfectly valid at the end of the program.
[1]:
https://github.com/dlang/DIPs/blob/master/DIPs/DIP1040.md#move-constructor
[2]:
https://github.com/dlang/DIPs/blob/master/DIPs/DIP1040.md#assignment-after-move
[3]: https://issues.dlang.org/show_bug.cgi?id=20424
More information about the Digitalmars-d
mailing list