ANSI vs Cfront binding
Ross Alexander
rossa@stimpy.math.auckland.ac.nz
Mon Dec 1 13:23:00 GMT 1997
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 <iostream.h>
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 );
}
More information about the Gcc
mailing list