source: src/tests/raii/memberCtors.c @ 2472a19

new-envwith_gc
Last change on this file since 2472a19 was 80e8582, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Move constructor/destructor tests to raii subdirectory

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[4d4882a]1struct WrappedInt {
2  int x;
3};
4
[2afec66]5void ?{}(WrappedInt & this) {
[4d4882a]6  printf("constructing int\n");
[2afec66]7  this.x = 0;
[4d4882a]8}
9
[2afec66]10void ?{}(WrappedInt & this, WrappedInt other) {
[4d4882a]11  printf("copy constructing int: %d\n", other.x);
[2afec66]12  this.x = other.x;
[4d4882a]13}
14
[2afec66]15void ?{}(WrappedInt & this, int x) {
[4d4882a]16  printf("constructing int: %d\n", x);
[2afec66]17  this.x = x;
[4d4882a]18}
19
[2afec66]20void ^?{}(WrappedInt & this) {
21  printf("destructing int: %d\n", this.x);
[4d4882a]22}
23
[2afec66]24void ?=?(WrappedInt & this, int x) {
25  printf("assigning int: %d %d\n", this.x, x);
26  this.x = x;
[4d4882a]27}
28
29struct A {
30  WrappedInt x, y, z;
31};
32
[2afec66]33void ?{}(A & a) {
[235114f]34  // currently must define default ctor, since there's no "= default" syntax
35}
36
[2afec66]37void ?{}(A & a, int x) {
[f7e749f]38  printf("begin construct A\n");
[2afec66]39  printf("construct a.x\n");
40  (a.x){ x+999 };
41  printf("assign a.y\n");
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
[2afec66]46void ?{}(A & this, A other) {
[f7e749f]47  printf("begin copy construct A\n");
[2afec66]48  printf("copy construct this.x\n");
49  (this.x){ other.x };
50  printf("assign this.y\n");
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
[2afec66]55A ?=?(A & this, A other) {
[ddbde34]56  printf("begin ?=? A\n");
[2afec66]57  this.x = other.x;
58  this.y = other.y;
59  this.z = other.z;
[ddbde34]60  printf("end ?=? A\n");
[2afec66]61  return this;
[ddbde34]62}
63
[4d4882a]64struct B {
65  A a1, a2, a3;
66};
67
[2afec66]68void ?{}(B & b) {
[f7e749f]69  printf("begin construct B\n");
[2afec66]70  printf("assign b.a2\n");
71  b.a2 = (A) { 2 };
72  printf("construct b.a1\n");
73  (b.a1){ 1 };
[4d4882a]74#ifdef ERR1
[2afec66]75  (b.a2){ b.a3 }; // error, b->a2 was used previously but is explicitly constructed
[4d4882a]76#endif
[f7e749f]77  printf("end construct B\n");
[4d4882a]78} // a2, a3 never constructed - will be automatically default constructed
79
[2afec66]80void ^?{}(B & b) {
81  b.a2 = (A) { 0 };
82  ^(b.a1){};
[4d4882a]83} // a2, a3 never destructed - will be automatically destructed
84
85int main() {
[44f6341]86  printf("Before declaration of b1\n");
[4d4882a]87  B b1;
[44f6341]88  printf("Before declaration of b2\n");
[4d4882a]89  B b2 = b1;
[44f6341]90  printf("End of main\n");
[4d4882a]91}
Note: See TracBrowser for help on using the repository browser.