| [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; } | 
|---|
| [12536d3] | 21 | void ?{}(Y & y, int i) { printf("Value constructing a Y %d\n", i); y.i = i; } | 
|---|
| [a8555c5] | 22 |  | 
|---|
|  | 23 | struct X { Y & r; Y y; }; | 
|---|
|  | 24 | void ?{}(X & x) { | 
|---|
|  | 25 | // ensure that r is not implicitly constructed | 
|---|
|  | 26 | } | 
|---|
|  | 27 | void ?{}(X & x, X other) { | 
|---|
|  | 28 | // ensure that r is not implicitly constructed | 
|---|
|  | 29 | } | 
|---|
|  | 30 | void ^?{}(X & x) { | 
|---|
|  | 31 | // ensure that r is not implicitly destructed | 
|---|
|  | 32 | } | 
|---|
|  | 33 | X ?=?(X & x, X other) { return x; } | 
|---|
|  | 34 |  | 
|---|
| [12536d3] | 35 | // ensure that generated functions do not implicitly operate on references | 
|---|
|  | 36 | struct Z { Y & r; Y y; }; | 
|---|
|  | 37 |  | 
|---|
| [a8555c5] | 38 | // test user-defined reference-returning function | 
|---|
|  | 39 | int & toref( int * p ) { return *p; } | 
|---|
|  | 40 | // test user-defined reference-parameter function | 
|---|
|  | 41 | int * toptr( int & r ) { return &r; } | 
|---|
|  | 42 |  | 
|---|
| [b05a4eb] | 43 | void changeRef( int & r ) { | 
|---|
|  | 44 | r++; | 
|---|
|  | 45 | } | 
|---|
|  | 46 |  | 
|---|
| [a8555c5] | 47 | int main() { | 
|---|
|  | 48 | int x = 123456, *p1 = &x, **p2 = &p1, ***p3 = &p2, | 
|---|
|  | 49 | &r1 = x,    &&r2 = r1,   &&&r3 = r2; | 
|---|
|  | 50 | ***p3 = 3;                          // change x | 
|---|
| [0415e24] | 51 | // ((int&)r3 = 3;                      // change x, ***r3 | 
|---|
| [a8555c5] | 52 | **p3 = &x;                          // change p1 | 
|---|
| [0415e24] | 53 | // ((int*&)&r3) = &x;                  // change r1, (&*)**r3 | 
|---|
| [a8555c5] | 54 | *p3 = &p1;                          // change p2 | 
|---|
| [0415e24] | 55 | // ((int**&)&&r3) = &p2;               // change r2, (&(&*)*)*r3 | 
|---|
|  | 56 | // ((int***&)&&&r3) = p3;              // change r3 to p3, (&(&(&*)*)*)r3 | 
|---|
| [b05a4eb] | 57 | int y = 0, z = 11, & ar[3] = { x, y, z };    // initialize array of references | 
|---|
| [0415e24] | 58 | // &ar[1] = &z;                        // change reference array element | 
|---|
|  | 59 | // typeof( ar[1] ) p;                  // is int, i.e., the type of referenced object | 
|---|
|  | 60 | // typeof( &ar[1] ) q;                 // is int &, i.e., the type of reference | 
|---|
|  | 61 | // sizeof( ar[1] ) == sizeof( int );   // is true, i.e., the size of referenced object | 
|---|
|  | 62 | // sizeof( &ar[1] ) == sizeof( int *); // is true, i.e., the size of a reference | 
|---|
| [a8555c5] | 63 |  | 
|---|
|  | 64 | // test that basic reference properties are true - r1 should be an alias for x | 
|---|
|  | 65 | printf("%d %d %d\n", x, r1, &x == &r1); | 
|---|
|  | 66 | r1 = 12; | 
|---|
|  | 67 | printf("%d %d %d\n", x, r1, &x == &r1); | 
|---|
|  | 68 |  | 
|---|
|  | 69 | // test that functions using basic references work | 
|---|
|  | 70 | printf("%d %d %d %d\n", toref(&x), toref(p1), toptr(r1) == toptr(x), toptr(r1) == &x); | 
|---|
|  | 71 |  | 
|---|
| [b05a4eb] | 72 | changeRef( x ); | 
|---|
|  | 73 | changeRef( y ); | 
|---|
|  | 74 | changeRef( z ); | 
|---|
|  | 75 | printf("%d %d %d\n", x, y, z); | 
|---|
|  | 76 | changeRef( r1 ); | 
|---|
|  | 77 | printf("%d %d\n", r1, x); | 
|---|
|  | 78 |  | 
|---|
| [a8555c5] | 79 | // test that reference members are not implicitly constructed/destructed/assigned | 
|---|
|  | 80 | X x1, x2 = x1; | 
|---|
|  | 81 | x1 = x2; | 
|---|
| [12536d3] | 82 |  | 
|---|
|  | 83 | Z z1, z2 = z1; | 
|---|
|  | 84 | Y z1r = 56, z2r = 78; | 
|---|
|  | 85 | &z1.r = &z1r; | 
|---|
|  | 86 | &z2.r = &z2r; | 
|---|
| [0415e24] | 87 |  | 
|---|
| [12536d3] | 88 | z1 = z2; | 
|---|
| [12db9e4] | 89 |  | 
|---|
|  | 90 | // test rvalue-to-reference conversion | 
|---|
|  | 91 | { | 
|---|
|  | 92 | struct S { double x, y; }; | 
|---|
|  | 93 | void f( int & i, int & j, S & s, int v[] ) { | 
|---|
|  | 94 | printf("%d %d { %g, %g }, [%d, %d, %d]\n", i, j, s.[x, y], v[0], v[1], v[2]); | 
|---|
|  | 95 | } | 
|---|
|  | 96 | void g(int & i) { printf("%d\n", i); } | 
|---|
|  | 97 | void h(int &&& i) { printf("%d\n", i); } | 
|---|
|  | 98 |  | 
|---|
|  | 99 | int &&& r = 3;  // rvalue to reference | 
|---|
|  | 100 | int i = r; | 
|---|
|  | 101 | printf("%d %d\n", i, r);  // both 3 | 
|---|
|  | 102 |  | 
|---|
|  | 103 | g( 3 );          // rvalue to reference | 
|---|
|  | 104 | h( (int &&&)3 ); // rvalue to reference | 
|---|
|  | 105 |  | 
|---|
|  | 106 | int a = 5, b = 4; | 
|---|
|  | 107 | f( 3, a + b, (S){ 1.0, 7.0 }, (int [3]){ 1, 2, 3 } ); // two rvalue to reference | 
|---|
|  | 108 | } | 
|---|
| [a8555c5] | 109 | } | 
|---|
|  | 110 |  | 
|---|
|  | 111 | // Local Variables: // | 
|---|
|  | 112 | // tab-width: 4 // | 
|---|
|  | 113 | // End: // | 
|---|
|  | 114 |  | 
|---|