| 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 |  | 
|---|
| 39 | int main() { | 
|---|
| 40 | int x = 123456, *p1 = &x, **p2 = &p1, ***p3 = &p2, | 
|---|
| 41 | &r1 = x,    &&r2 = r1,   &&&r3 = r2; | 
|---|
| 42 | ***p3 = 3;                          // change x | 
|---|
| 43 | **p3 = &x;                          // change p1 | 
|---|
| 44 | *p3 = &p1;                          // change p2 | 
|---|
| 45 | int y, z, & ar[3] = { x, y, z };    // initialize array of references | 
|---|
| 46 |  | 
|---|
| 47 | // test that basic reference properties are true - r1 should be an alias for x | 
|---|
| 48 | printf("%d %d %d\n", x, r1, &x == &r1); | 
|---|
| 49 | r1 = 12; | 
|---|
| 50 | printf("%d %d %d\n", x, r1, &x == &r1); | 
|---|
| 51 |  | 
|---|
| 52 | // test that functions using basic references work | 
|---|
| 53 | printf("%d %d %d %d\n", toref(&x), toref(p1), toptr(r1) == toptr(x), toptr(r1) == &x); | 
|---|
| 54 |  | 
|---|
| 55 | // test that reference members are not implicitly constructed/destructed/assigned | 
|---|
| 56 | X x1, x2 = x1; | 
|---|
| 57 | x1 = x2; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | // Local Variables: // | 
|---|
| 61 | // tab-width: 4 // | 
|---|
| 62 | // End: // | 
|---|
| 63 |  | 
|---|