Changeset 6c7fe7b
- Timestamp:
- Jun 30, 2023, 4:50:01 PM (17 months ago)
- Branches:
- master
- Children:
- c5e2a84
- Parents:
- d7874052
- Location:
- tests/raii
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
tests/raii/.expect/memberCtors-ERR1.txt
rd7874052 r6c7fe7b 1 raii/memberCtors.cfa:9 2:1 error: in ?{}, field a2 used before being constructed1 raii/memberCtors.cfa:94:1 error: in ?{}, field a2 used before being constructed -
tests/raii/memberCtors.cfa
rd7874052 r6c7fe7b 1 #include <fstream.hfa> 2 1 3 struct WrappedInt { 2 3 4 int x; 5 int id; 4 6 }; 5 7 int intID = 0; 6 8 7 void ?{}( WrappedInt & this) {8 9 printf("constructing int id: %d\n", this.id);10 9 void ?{}( WrappedInt & this ) { 10 this.id = intID++; 11 sout | "constructing int id: " | this.id; 12 this.x = 0; 11 13 } 12 14 13 void ?{}( WrappedInt & this, WrappedInt other) {14 15 printf("copy constructing int: %d id: %d\n", other.x, this.id);16 15 void ?{}( WrappedInt & this, WrappedInt other ) { 16 this.id = intID++; 17 sout | "copy constructing int: " | other.x | "id: " | this.id; 18 this.x = other.x; 17 19 } 18 20 19 void ?{}( WrappedInt & this, int x) {20 21 printf("constructing int: %d id: %d\n", x, this.id);22 21 void ?{}( WrappedInt & this, int x ) { 22 this.id = intID++; 23 sout | "constructing int: " | x | "id: " | this.id; 24 this.x = x; 23 25 } 24 26 25 void ^?{}( WrappedInt & this) {26 printf("destructing int: %d id: %d\n", this.x, this.id);27 void ^?{}( WrappedInt & this ) { 28 sout | "destructing int: " | this.x | "id: " | this.id; 27 29 } 28 30 29 /* WrappedInt */ void ?=?( WrappedInt & this, int x) {30 printf("assigning int: %d %d id: %d\n", this.x, x, this.id);31 32 31 /* WrappedInt */ void ?=?( WrappedInt & this, int x ) { 32 sout | "assigning int: " | this.x | x | "id: " | this.id; 33 this.x = x; 34 // return this; 33 35 } 34 36 35 // WrappedInt ?=?( WrappedInt & this, WrappedInt other) {36 // printf("assigning int: %d %d\n", this.x, other.x);37 // WrappedInt ?=?( WrappedInt & this, WrappedInt other ) { 38 // sout | "assigning int: " | this.x | other.x; 37 39 // this.x = other.x; 38 40 // return this; … … 40 42 41 43 struct A { 42 43 44 WrappedInt x, y, z; 45 int id; 44 46 }; 45 47 int AID = 0; 46 48 47 void ?{}( A & a) {48 49 50 printf("default construct A %d\n", a.id);49 void ?{}( A & a ) { 50 // currently must define default ctor, since there's no "= default" syntax 51 a.id = AID++; 52 sout | "default construct A" | a.id; 51 53 } 52 54 53 void ?{}( A & a, int x) {54 55 printf("begin construct A id: %d\n", a.id);56 printf("construct a.x\n");57 58 printf("assign a.y\n");59 60 printf("end construct A\n");55 void ?{}( A & a, int x ) { 56 a.id = AID++; 57 sout | "begin construct A id: " | a.id; 58 sout | "construct a.x"; 59 (a.x){ x+999 }; 60 sout | "assign a.y"; 61 a.y = 0; // not a constructor - default constructor will be inserted 62 sout | "end construct A"; 61 63 } // z never constructed - will be automatically default constructed 62 64 63 void ?{}( A & this, A other) {64 65 printf("begin copy construct A id: %d\n", this.id);66 printf("copy construct this.x\n");67 68 printf("assign this.y\n");69 70 printf("end copy construct A\n");65 void ?{}( A & this, A other ) { 66 this.id = AID++; 67 sout | "begin copy construct A id: " | this.id; 68 sout | "copy construct this.x"; 69 (this.x){ other.x }; 70 sout | "assign this.y"; 71 this.y = other.y; // not a constructor - copy constructor will be inserted 72 sout | "end copy construct A"; 71 73 } // z never constructed - will be automatically copy constructed 72 74 73 A ?=?( A & this, A other) {74 printf("begin ?=? A id: %d\n", this.id);75 76 77 78 printf("end ?=? A\n");79 75 A ?=?( A & this, A other ) { 76 sout | "begin ?=? A id: " | this.id; 77 this.x = other.x; 78 this.y = other.y; 79 this.z = other.z; 80 sout | "end ?=? A"; 81 return this; 80 82 } 81 83 82 84 struct B { 83 84 85 A a1, a2, a3; 86 int id; 85 87 }; 86 88 int BID = 0; 87 89 88 void ?{}( B & b) {89 90 printf("begin construct B id: %d\n", b.id);91 printf("assign b.a2\n");92 b.a2 = (A){ 2 };93 printf("construct b.a1\n");94 90 void ?{}( B & b ) { 91 b.id = BID++; 92 sout | "begin construct B id: " | b.id; 93 sout | "assign b.a2"; 94 b.a2 = (A){ 2 }; 95 sout | "construct b.a1"; 96 (b.a1){ 1 }; 95 97 #ifdef ERR1 96 98 (b.a2){ b.a3 }; // error, b->a2 was used previously but is explicitly constructed 97 99 #endif 98 printf("end construct B\n");100 sout | "end construct B"; 99 101 } // a2, a3 never constructed - will be automatically default constructed 100 102 101 void ^?{}( B & b) {102 103 printf("begin destruct B id: %d\n", b.id);104 105 106 printf("end destruct B\n");103 void ^?{}( B & b ) { 104 b.id = BID++; 105 sout | "begin destruct B id: " | b.id; 106 b.a2 = (A) { 0 }; 107 ^(b.a1){}; 108 sout | "end destruct B"; 107 109 } // a2, a3 never destructed - will be automatically destructed 108 110 109 111 int main() { 110 printf("Before declaration of b1\n");111 112 printf("Before declaration of b2\n");113 114 printf("End of main\n");112 sout | "Before declaration of b1"; 113 B b1; // b1 = { { 1000, 0, 0 }, { 1001, 0, 0 }, { 0, 0, 0 } } 114 sout | "Before declaration of b2"; 115 B b2 = b1; 116 sout | "End of main"; 115 117 }
Note: See TracChangeset
for help on using the changeset viewer.