[4d4882a] | 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 | void ?=?(WrappedInt * this, int x) { |
---|
| 25 | printf("assigning int: %d %d\n", this->x, x); |
---|
| 26 | this->x = x; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | struct A { |
---|
| 30 | WrappedInt x, y, z; |
---|
| 31 | }; |
---|
| 32 | |
---|
[235114f] | 33 | void ?{}(A * a) { |
---|
| 34 | // currently must define default ctor, since there's no "= default" syntax |
---|
| 35 | } |
---|
| 36 | |
---|
[4d4882a] | 37 | void ?{}(A * a, int x) { |
---|
[f7e749f] | 38 | printf("begin construct A\n"); |
---|
| 39 | printf("construct a->x\n"); |
---|
[4d4882a] | 40 | (&a->x){ x+999 }; |
---|
[f7e749f] | 41 | printf("assign a->y\n"); |
---|
[4d4882a] | 42 | a->y = 0; // not a constructor - default constructor will be inserted |
---|
[f7e749f] | 43 | printf("end construct A\n"); |
---|
[4d4882a] | 44 | } // z never constructed - will be automatically default constructed |
---|
| 45 | |
---|
| 46 | void ?{}(A * this, A other) { |
---|
[f7e749f] | 47 | printf("begin copy construct A\n"); |
---|
| 48 | printf("copy construct this->x\n"); |
---|
[4d4882a] | 49 | (&this->x){ other.x }; |
---|
[f7e749f] | 50 | printf("assign this->y\n"); |
---|
[4d4882a] | 51 | this->y = other.y; // not a constructor - copy constructor will be inserted |
---|
[f7e749f] | 52 | printf("end copy construct A\n"); |
---|
[4d4882a] | 53 | } // z never constructed - will be automatically copy constructed |
---|
| 54 | |
---|
| 55 | struct B { |
---|
| 56 | A a1, a2, a3; |
---|
| 57 | }; |
---|
| 58 | |
---|
| 59 | void ?{}(B * b) { |
---|
[f7e749f] | 60 | printf("begin construct B\n"); |
---|
| 61 | printf("assign b->a2\n"); |
---|
[4d4882a] | 62 | b->a2 = (A) { 2 }; |
---|
[f7e749f] | 63 | printf("construct b->a1\n"); |
---|
[4d4882a] | 64 | (&b->a1){ 1 }; |
---|
| 65 | #ifdef ERR1 |
---|
| 66 | (&b->a2){ b->a3 }; // error, b->a2 was used previously but is explicitly constructed |
---|
| 67 | #endif |
---|
[f7e749f] | 68 | printf("end construct B\n"); |
---|
[4d4882a] | 69 | } // a2, a3 never constructed - will be automatically default constructed |
---|
| 70 | |
---|
| 71 | void ^?{}(B * b) { |
---|
| 72 | b->a2 = (A) { 0 }; |
---|
| 73 | ^(&b->a1){}; |
---|
| 74 | } // a2, a3 never destructed - will be automatically destructed |
---|
| 75 | |
---|
| 76 | int main() { |
---|
[44f6341] | 77 | printf("Before declaration of b1\n"); |
---|
[4d4882a] | 78 | B b1; |
---|
[44f6341] | 79 | printf("Before declaration of b2\n"); |
---|
[4d4882a] | 80 | B b2 = b1; |
---|
[44f6341] | 81 | printf("End of main\n"); |
---|
[4d4882a] | 82 | } |
---|