From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ross Alexander To: egcs@cygnus.com Subject: ANSI vs Cfront binding Date: Mon, 01 Dec 1997 13:23:00 -0000 Message-id: <199712012122.KAA31271@stimpy.math.auckland.ac.nz> X-SW-Source: 1997-12/msg00061.html A friend of mine showed me this piece of code. I was interesting on any comments people had and what should be expected in the future (with respect to ANSI/ISO compliance). ---------------------------------------------------------------------- // Demonstration of difference between ANSI and Cfront rules // for binding references. Program prints "Cfront" when compiled // with Cfront rules, and "ANSI" when compiled by ANSI rules. #include char x, y; char *global_ptr; void Test( const char *const &ref ) { // Caller initialized ref to global_ptr, possible via a temporary. // Now change the value of global_ptr. global_ptr = &y; if( ref==&x ) { // Referenced pointer did not change - must be a temporary. cout << "ANSI\n"; } if( ref==&y ) { // Referenced pointer changed -- must be directly bound. cout << "Cfront\n"; } } main() { global_ptr = &x; // Under Cfront rules, the formal parameter ref is bound directly to // global_ptr. Under ANSI rules, the formal parameter ref is bound // to a temporary copy of global_ptr. Why the temporary? Because // direct binding is allowed [dcl.init.ref] only if the formal and // actual are "reference compatible". The ANSI rules allow only // top-level changes in qualifiers for making lvalues reference compatible. // No temporary would be generated if the 2nd-level qualifiers were changed // to match. I.e., if global_ptr were of type (const char *) or // ref were of type (char *const &). Test( global_ptr ); }