[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 |
---|
[f498c51] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Tue Dec 25 14:31:48 2018 |
---|
| 13 | // Update Count : 11 |
---|
[a8555c5] | 14 | // |
---|
| 15 | |
---|
[f498c51] | 16 | #include <fstream.hfa> |
---|
| 17 | |
---|
[a8555c5] | 18 | struct Y { int i; }; |
---|
[f498c51] | 19 | void ?{}( Y & y ) { sout | "Default constructing a Y"; } |
---|
| 20 | void ?{}( Y & y, Y other ) { sout | "Copy constructing a Y"; } |
---|
| 21 | void ^?{}( Y & y ) { sout | "Destructing a Y"; } |
---|
| 22 | Y ?=?( Y & y, Y other ) { sout | "Assigning a Y"; return y; } |
---|
| 23 | void ?{}( Y & y, int i ) { sout | "Value constructing a Y" | i; y.i = i; } |
---|
[a8555c5] | 24 | |
---|
| 25 | struct X { Y & r; Y y; }; |
---|
[f498c51] | 26 | void ?{}( X & x ) { |
---|
[a8555c5] | 27 | // ensure that r is not implicitly constructed |
---|
| 28 | } |
---|
[f498c51] | 29 | void ?{}( X & x, X other ) { |
---|
[a8555c5] | 30 | // ensure that r is not implicitly constructed |
---|
| 31 | } |
---|
[f498c51] | 32 | void ^?{}( X & x ) { |
---|
[a8555c5] | 33 | // ensure that r is not implicitly destructed |
---|
| 34 | } |
---|
[f498c51] | 35 | X ?=?( X & x, X other ) { return x; } |
---|
[a8555c5] | 36 | |
---|
[12536d3] | 37 | // ensure that generated functions do not implicitly operate on references |
---|
| 38 | struct Z { Y & r; Y y; }; |
---|
| 39 | |
---|
[a8555c5] | 40 | // test user-defined reference-returning function |
---|
| 41 | int & toref( int * p ) { return *p; } |
---|
| 42 | // test user-defined reference-parameter function |
---|
| 43 | int * toptr( int & r ) { return &r; } |
---|
| 44 | |
---|
[b05a4eb] | 45 | void changeRef( int & r ) { |
---|
| 46 | r++; |
---|
| 47 | } |
---|
| 48 | |
---|
[a8555c5] | 49 | int main() { |
---|
[101a4d2] | 50 | int x = 123456, x2 = 789, *p1 = &x, **p2 = &p1, ***p3 = &p2, |
---|
[a8555c5] | 51 | &r1 = x, &&r2 = r1, &&&r3 = r2; |
---|
[f498c51] | 52 | ***p3 = 3; // change x |
---|
| 53 | **p3 = &x; // change p1 |
---|
| 54 | *p3 = &p1; // change p2 |
---|
| 55 | int y = 0, z = 11, & ar[3] = { x, y, z }; // initialize array of references |
---|
| 56 | // &ar[1] = &z; // change reference array element |
---|
| 57 | // typeof( ar[1] ) p = 3; // is int, i.e., the type of referenced object |
---|
| 58 | // typeof( &ar[1] ) q = &x; // is int *, i.e., the type of pointer |
---|
[a02842f] | 59 | // _Static_assert( sizeof( ar[1] ) == sizeof( int ), "Array type should be int." ); // is true, i.e., the size of referenced object |
---|
| 60 | // _Static_assert( sizeof( &ar[1] ) == sizeof( int *), "Address of array should be int *." ); // is true, i.e., the size of a reference |
---|
[a8555c5] | 61 | |
---|
[f498c51] | 62 | ((int*&)&r3) = &x; // change r1, (&*)**r3 |
---|
[101a4d2] | 63 | x = 3; |
---|
[a8555c5] | 64 | // test that basic reference properties are true - r1 should be an alias for x |
---|
[f498c51] | 65 | sout | x | r1 | &x == &r1; |
---|
[a8555c5] | 66 | r1 = 12; |
---|
[f498c51] | 67 | sout | x | r1 | &x == &r1; |
---|
[a8555c5] | 68 | |
---|
| 69 | // test that functions using basic references work |
---|
[f498c51] | 70 | sout | toref( &x ) | toref( p1 ) | toptr( r1 ) == toptr( x ) | toptr( r1 ) == &x; |
---|
[a8555c5] | 71 | |
---|
[b05a4eb] | 72 | changeRef( x ); |
---|
| 73 | changeRef( y ); |
---|
| 74 | changeRef( z ); |
---|
[f498c51] | 75 | sout | x | y | z; |
---|
[b05a4eb] | 76 | changeRef( r1 ); |
---|
[f498c51] | 77 | sout | r1 | x; |
---|
| 78 | |
---|
| 79 | r3 = 6; // change x, ***r3 |
---|
| 80 | sout | "x = " | x | " ; x2 = " | x2; // check that x was changed |
---|
| 81 | &r3 = &x2; // change r1 to refer to x2, (&*)**r3 |
---|
| 82 | r3 = 999; // modify x2 |
---|
| 83 | sout | "x = " | x | " ; x2 = " | x2; // check that x2 was changed |
---|
| 84 | ((int**&)&&r3) = p2; // change r2, (&(&*)*)*r3, ensure explicit cast to reference works |
---|
| 85 | r3 = 12345; // modify x |
---|
| 86 | sout | "x = " | x | " ; x2 = " | x2; // check that x was changed |
---|
| 87 | &&&r3 = p3; // change r3 to p3, (&(&(&*)*)*)r3 |
---|
| 88 | ((int&)r3) = 22222; // modify x, ensure explicit cast to reference works |
---|
| 89 | sout | "x = " | x | " ; x2 = " | x2; // check that x was changed |
---|
[101a4d2] | 90 | |
---|
[a8555c5] | 91 | // test that reference members are not implicitly constructed/destructed/assigned |
---|
| 92 | X x1, x2 = x1; |
---|
| 93 | x1 = x2; |
---|
[12536d3] | 94 | |
---|
| 95 | Z z1, z2 = z1; |
---|
| 96 | Y z1r = 56, z2r = 78; |
---|
| 97 | &z1.r = &z1r; |
---|
| 98 | &z2.r = &z2r; |
---|
[0415e24] | 99 | |
---|
[12536d3] | 100 | z1 = z2; |
---|
[12db9e4] | 101 | |
---|
| 102 | // test rvalue-to-reference conversion |
---|
| 103 | { |
---|
| 104 | struct S { double x, y; }; |
---|
| 105 | void f( int & i, int & j, S & s, int v[] ) { |
---|
[f498c51] | 106 | sout | i | j | "{ " | s.[x, y] | " }," | "[" | v[0] | "," | v[1] | "," | v[2] | "]"; |
---|
[12db9e4] | 107 | } |
---|
[f498c51] | 108 | void g(int & i) { sout | i; } |
---|
| 109 | void h(int &&& i) { sout | i; } |
---|
[12db9e4] | 110 | |
---|
[f498c51] | 111 | int &&& r = 3; // rvalue to reference |
---|
[12db9e4] | 112 | int i = r; |
---|
[f498c51] | 113 | sout | i | r; // both 3 |
---|
[12db9e4] | 114 | |
---|
[f498c51] | 115 | g( 3 ); // rvalue to reference |
---|
| 116 | h( (int &&&)3 ); // rvalue to reference |
---|
[12db9e4] | 117 | |
---|
| 118 | int a = 5, b = 4; |
---|
| 119 | f( 3, a + b, (S){ 1.0, 7.0 }, (int [3]){ 1, 2, 3 } ); // two rvalue to reference |
---|
| 120 | } |
---|
[a8555c5] | 121 | } |
---|
| 122 | |
---|
| 123 | // Local Variables: // |
---|
| 124 | // tab-width: 4 // |
---|
| 125 | // End: // |
---|