Move Constructor Syntax
Salih Dincer
salihdb at hotmail.com
Tue Oct 15 22:48:08 UTC 2024
On Tuesday, 15 October 2024 at 19:29:11 UTC, Arafel wrote:
>
> ```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.
The object pointed to by s1 lives, but is destroyed if another
object (`S(41)`) is pointed to by s1. Also, when you don't do
`@disable this();`, objects s1 and s2 will be created twice and
count == 3. For example:
```d
import std.stdio;
struct S
{
int i;
this (int i) { this.i = i; }
S* s;
this (ref S s) { this.s = &s; }
@disable this();
~this() { ++count; }
}
int count;
enum value = 42;
void main()
{
auto s1 = S(value);
auto s2 = S(s1);
s1.tupleof.writeln(": ", &s1); //42null: 7FFD6592A190
assert(s2.s.i == value);
s1 = S(41);
assert(s2.s.i != value); // because value is 41
assert(count == 1);
}
```
SDB at 79
More information about the Digitalmars-d
mailing list