public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Ross Alexander <rossa@stimpy.math.auckland.ac.nz>
To: egcs@cygnus.com
Subject: ANSI vs Cfront binding
Date: Mon, 01 Dec 1997 13:23:00 -0000	[thread overview]
Message-ID: <199712012122.KAA31271@stimpy.math.auckland.ac.nz> (raw)

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 );
}


                 reply	other threads:[~1997-12-01 13:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=199712012122.KAA31271@stimpy.math.auckland.ac.nz \
    --to=rossa@stimpy.math.auckland.ac.nz \
    --cc=egcs@cygnus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).