xxxxxxxxxx
struct Foo {
int payload;
this(int i)
{
this.payload = i;
}
this(ref return scope Foo that)
{
this = that; //cC = copyConstructor
logger ~= format("cC(%s)", that.payload);
}
Foo opUnary(string op)()
if(op == "++") {
++this.payload;
return this;
}
int getter()
{
return payload;
}
alias getter this;
}
import std.stdio, std.format;
string logger;
void main() {
Foo one = Foo(1), two = 2;
int[] arr = [one, two, Foo(3)];
auto three = ++two;/*
two.writeln(": 0x", &two.payload);//*/
//arr.writeln;/*
foreach(ref i; arr) {
i.writeln(": 0x", &i);
}//*/
writeln("0x", &three.payload);
logger.writeln;
} // end with the logger!
/*
Let be the structure Foo that wraps an int pointer. Let's setup Foo in 3 different ways:
1. Foo one = Foo(1);
2. Foo two = 2;
3. [ Foo(3) ];
Pretty clean, right? So why it's not run copy-constructor in 3? Also, when we write to the screen with writeln(), why four times copy-constructor are running?
*/