[a8555c5] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // references.c -- |
---|
| 8 | // |
---|
| 9 | // Author : Rob Schluntz |
---|
| 10 | // Created On : Wed Aug 23 16:11:50 2017 |
---|
| 11 | // Last Modified By : Rob Schluntz |
---|
| 12 | // Last Modified On : Wed Aug 23 16:12:03 |
---|
| 13 | // Update Count : 2 |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | struct Y { int i; }; |
---|
| 17 | void ?{}(Y & y) { printf("Default constructing a Y\n"); } |
---|
| 18 | void ?{}(Y & y, Y other) { printf("Copy constructing a Y\n"); } |
---|
| 19 | void ^?{}(Y & y) { printf("Destructing a Y\n"); } |
---|
| 20 | Y ?=?(Y & y, Y other) { printf("Assigning a Y\n"); return y; } |
---|
| 21 | |
---|
| 22 | struct X { Y & r; Y y; }; |
---|
| 23 | void ?{}(X & x) { |
---|
| 24 | // ensure that r is not implicitly constructed |
---|
| 25 | } |
---|
| 26 | void ?{}(X & x, X other) { |
---|
| 27 | // ensure that r is not implicitly constructed |
---|
| 28 | } |
---|
| 29 | void ^?{}(X & x) { |
---|
| 30 | // ensure that r is not implicitly destructed |
---|
| 31 | } |
---|
| 32 | X ?=?(X & x, X other) { return x; } |
---|
| 33 | |
---|
| 34 | // test user-defined reference-returning function |
---|
| 35 | int & toref( int * p ) { return *p; } |
---|
| 36 | // test user-defined reference-parameter function |
---|
| 37 | int * toptr( int & r ) { return &r; } |
---|
| 38 | |
---|
[b05a4eb] | 39 | void changeRef( int & r ) { |
---|
| 40 | r++; |
---|
| 41 | } |
---|
| 42 | |
---|
[a8555c5] | 43 | int main() { |
---|
| 44 | int x = 123456, *p1 = &x, **p2 = &p1, ***p3 = &p2, |
---|
| 45 | &r1 = x, &&r2 = r1, &&&r3 = r2; |
---|
| 46 | ***p3 = 3; // change x |
---|
| 47 | **p3 = &x; // change p1 |
---|
| 48 | *p3 = &p1; // change p2 |
---|
[b05a4eb] | 49 | int y = 0, z = 11, & ar[3] = { x, y, z }; // initialize array of references |
---|
[a8555c5] | 50 | |
---|
| 51 | // test that basic reference properties are true - r1 should be an alias for x |
---|
| 52 | printf("%d %d %d\n", x, r1, &x == &r1); |
---|
| 53 | r1 = 12; |
---|
| 54 | printf("%d %d %d\n", x, r1, &x == &r1); |
---|
| 55 | |
---|
| 56 | // test that functions using basic references work |
---|
| 57 | printf("%d %d %d %d\n", toref(&x), toref(p1), toptr(r1) == toptr(x), toptr(r1) == &x); |
---|
| 58 | |
---|
[b05a4eb] | 59 | changeRef( x ); |
---|
| 60 | changeRef( y ); |
---|
| 61 | changeRef( z ); |
---|
| 62 | printf("%d %d %d\n", x, y, z); |
---|
| 63 | changeRef( r1 ); |
---|
| 64 | printf("%d %d\n", r1, x); |
---|
| 65 | |
---|
[a8555c5] | 66 | // test that reference members are not implicitly constructed/destructed/assigned |
---|
| 67 | X x1, x2 = x1; |
---|
| 68 | x1 = x2; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | // Local Variables: // |
---|
| 72 | // tab-width: 4 // |
---|
| 73 | // End: // |
---|
| 74 | |
---|