| 1 | struct WrappedInt {
|
|---|
| 2 | int x;
|
|---|
| 3 | };
|
|---|
| 4 |
|
|---|
| 5 | void ?{}(WrappedInt & this) {
|
|---|
| 6 | printf("constructing int\n");
|
|---|
| 7 | this.x = 0;
|
|---|
| 8 | }
|
|---|
| 9 |
|
|---|
| 10 | void ?{}(WrappedInt & this, WrappedInt other) {
|
|---|
| 11 | printf("copy constructing int: %d\n", other.x);
|
|---|
| 12 | this.x = other.x;
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | void ?{}(WrappedInt & this, int x) {
|
|---|
| 16 | printf("constructing int: %d\n", x);
|
|---|
| 17 | this.x = x;
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | void ^?{}(WrappedInt & this) {
|
|---|
| 21 | printf("destructing int: %d\n", this.x);
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | /* WrappedInt */ void ?=?(WrappedInt & this, int x) {
|
|---|
| 25 | printf("assigning int: %d %d\n", this.x, x);
|
|---|
| 26 | this.x = x;
|
|---|
| 27 | // return this;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | // WrappedInt ?=?(WrappedInt & this, WrappedInt other) {
|
|---|
| 31 | // printf("assigning int: %d %d\n", this.x, other.x);
|
|---|
| 32 | // this.x = other.x;
|
|---|
| 33 | // return this;
|
|---|
| 34 | // }
|
|---|
| 35 |
|
|---|
| 36 | struct A {
|
|---|
| 37 | WrappedInt x, y, z;
|
|---|
| 38 | };
|
|---|
| 39 |
|
|---|
| 40 | void ?{}(A & a) {
|
|---|
| 41 | // currently must define default ctor, since there's no "= default" syntax
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | void ?{}(A & a, int x) {
|
|---|
| 45 | printf("begin construct A\n");
|
|---|
| 46 | printf("construct a.x\n");
|
|---|
| 47 | (a.x){ x+999 };
|
|---|
| 48 | printf("assign a.y\n");
|
|---|
| 49 | a.y = 0; // not a constructor - default constructor will be inserted
|
|---|
| 50 | printf("end construct A\n");
|
|---|
| 51 | } // z never constructed - will be automatically default constructed
|
|---|
| 52 |
|
|---|
| 53 | void ?{}(A & this, A other) {
|
|---|
| 54 | printf("begin copy construct A\n");
|
|---|
| 55 | printf("copy construct this.x\n");
|
|---|
| 56 | (this.x){ other.x };
|
|---|
| 57 | printf("assign this.y\n");
|
|---|
| 58 | this.y = other.y; // not a constructor - copy constructor will be inserted
|
|---|
| 59 | printf("end copy construct A\n");
|
|---|
| 60 | } // z never constructed - will be automatically copy constructed
|
|---|
| 61 |
|
|---|
| 62 | A ?=?(A & this, A other) {
|
|---|
| 63 | printf("begin ?=? A\n");
|
|---|
| 64 | this.x = other.x;
|
|---|
| 65 | this.y = other.y;
|
|---|
| 66 | this.z = other.z;
|
|---|
| 67 | printf("end ?=? A\n");
|
|---|
| 68 | return this;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | struct B {
|
|---|
| 72 | A a1, a2, a3;
|
|---|
| 73 | };
|
|---|
| 74 |
|
|---|
| 75 | void ?{}(B & b) {
|
|---|
| 76 | printf("begin construct B\n");
|
|---|
| 77 | printf("assign b.a2\n");
|
|---|
| 78 | b.a2 = (A) { 2 };
|
|---|
| 79 | printf("construct b.a1\n");
|
|---|
| 80 | (b.a1){ 1 };
|
|---|
| 81 | #ifdef ERR1
|
|---|
| 82 | (b.a2){ b.a3 }; // error, b->a2 was used previously but is explicitly constructed
|
|---|
| 83 | #endif
|
|---|
| 84 | printf("end construct B\n");
|
|---|
| 85 | } // a2, a3 never constructed - will be automatically default constructed
|
|---|
| 86 |
|
|---|
| 87 | void ^?{}(B & b) {
|
|---|
| 88 | printf("begin destruct B\n");
|
|---|
| 89 | b.a2 = (A) { 0 };
|
|---|
| 90 | ^(b.a1){};
|
|---|
| 91 | printf("end destruct B\n");
|
|---|
| 92 | } // a2, a3 never destructed - will be automatically destructed
|
|---|
| 93 |
|
|---|
| 94 | int main() {
|
|---|
| 95 | printf("Before declaration of b1\n");
|
|---|
| 96 | B b1; // b1 = { { 1000, 0, 0 }, { 1001, 0, 0 }, { 0, 0, 0 } }
|
|---|
| 97 | printf("Before declaration of b2\n");
|
|---|
| 98 | B b2 = b1;
|
|---|
| 99 | printf("End of main\n");
|
|---|
| 100 | }
|
|---|