Opened 11 months ago

#209 new defect

Mutable reference to constant is not forbidden

Reported by: mlbrooks Owned by:
Priority: minor Component: cfa-cc
Version: 1.0 Keywords:
Cc:

Description

void fred( int & i) {
    printf("fred got:  %d\n", i);
    i = 4;
    printf("fred left: %d\n", i);
}

int produce() {
    return 17;
}

int main() {
    const int i = 3;
    printf("main a:    %d\n", i);
    fred( i );                     // unsound
    printf("main b:    %d\n", i);  // i is changed!

    // we soundly allow a mutable reference to a temporary
    fred( produce() );
}

Actual: Compile succeeds, run prints:

main a:    3
fred got:  3
fred left: 4
main b:    4
fred got:  17
fred left: 4

Expected (minimal fix): Compile error, cannot get mutable reference to const i at "unsound" line

Further candidate feature request: Compile succeeds, run prints:

main a:    3
fred got:  3
fred left: 4
main b:    3
fred got:  17
fred left: 4

This bug was discovered during discssions about #189 and the 189 fix is not addressing this case.

Change History (0)

Note: See TracTickets for help on using tickets.