Ignore:
Timestamp:
Apr 25, 2018, 4:55:53 PM (4 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
new-env, with_gc
Children:
42107b4
Parents:
2efe4b8 (diff), 9d5fb67 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge remote-tracking branch 'origin/master' into with_gc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tests/references.c

    r2efe4b8 r1cdfa82  
    4646
    4747int main() {
    48         int x = 123456, *p1 = &x, **p2 = &p1, ***p3 = &p2,
     48        int x = 123456, x2 = 789, *p1 = &x, **p2 = &p1, ***p3 = &p2,
    4949                &r1 = x,    &&r2 = r1,   &&&r3 = r2;
    5050        ***p3 = 3;                          // change x
     
    5252        *p3 = &p1;                          // change p2
    5353        int y = 0, z = 11, & ar[3] = { x, y, z };    // initialize array of references
     54        // &ar[1] = &z;                        // change reference array element
     55        // typeof( ar[1] ) p;                  // is int, i.e., the type of referenced object
     56        // typeof( &ar[1] ) q;                 // is int &, i.e., the type of reference
     57        // sizeof( ar[1] ) == sizeof( int );   // is true, i.e., the size of referenced object
     58        // sizeof( &ar[1] ) == sizeof( int *); // is true, i.e., the size of a reference
    5459
     60        ((int*&)&r3) = &x;                  // change r1, (&*)**r3
     61        x = 3;
    5562        // test that basic reference properties are true - r1 should be an alias for x
    5663        printf("%d %d %d\n", x, r1, &x == &r1);
     
    6875        printf("%d %d\n", r1, x);
    6976
     77        r3 = 6;                               // change x, ***r3
     78        printf("x = %d ; x2 = %d\n", x, x2);  // check that x was changed
     79        &r3 = &x2;                            // change r1 to refer to x2, (&*)**r3
     80        r3 = 999;                             // modify x2
     81        printf("x = %d ; x2 = %d\n", x, x2);  // check that x2 was changed
     82        ((int**&)&&r3) = p2;                  // change r2, (&(&*)*)*r3, ensure explicit cast to reference works
     83        r3 = 12345;                           // modify x
     84        printf("x = %d ; x2 = %d\n", x, x2);  // check that x was changed
     85        &&&r3 = p3;                           // change r3 to p3, (&(&(&*)*)*)r3
     86        ((int&)r3) = 22222;                   // modify x, ensure explicit cast to reference works
     87        printf("x = %d ; x2 = %d\n", x, x2);  // check that x was changed
     88
    7089        // test that reference members are not implicitly constructed/destructed/assigned
    7190        X x1, x2 = x1;
     
    7695        &z1.r = &z1r;
    7796        &z2.r = &z2r;
     97
    7898        z1 = z2;
     99
     100        // test rvalue-to-reference conversion
     101        {
     102                struct S { double x, y; };
     103                void f( int & i, int & j, S & s, int v[] ) {
     104                        printf("%d %d { %g, %g }, [%d, %d, %d]\n", i, j, s.[x, y], v[0], v[1], v[2]);
     105                }
     106                void g(int & i) { printf("%d\n", i); }
     107                void h(int &&& i) { printf("%d\n", i); }
     108
     109                int &&& r = 3;  // rvalue to reference
     110                int i = r;
     111                printf("%d %d\n", i, r);  // both 3
     112
     113                g( 3 );          // rvalue to reference
     114                h( (int &&&)3 ); // rvalue to reference
     115
     116                int a = 5, b = 4;
     117                f( 3, a + b, (S){ 1.0, 7.0 }, (int [3]){ 1, 2, 3 } ); // two rvalue to reference
     118        }
    79119}
    80120
Note: See TracChangeset for help on using the changeset viewer.