🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1951

User-defined reference casts

#1951
From: Bryan Boreham
Date: Tue, 12 Sep 1995 14:49
50 lines
2219 bytes
I am having trouble deciding what the meaning of a cast of an object
to a reference should be, in the context of user-defined conversion
functions.  For example:

    class representation {
     public:
       int i;
    };

    class foo {
       int x;
       representation rep;
     public:
       operator const representation& () const;
       operator representation () const;
    };

    int main()
    {
       // What does this do?
       foo xx;
       representation &ref = (representation&)xx;
    }

I have tried it with a number of C++ compilers, which variously call
operator representation(), cast the address of xx, or give an error.
Those that call the operator would seem to be breaking the rule about
non-const references to temporaries; those that cast the address are
being somewhat unhelfpul: this code is surely a mistake by the
programmer.

My old copy of the ARM says [5.4] "conversion functions are not called
as the result of a cast to a reference", which flies in the face of
experience.  The same sort of wording appears in the DWP
[expr.static.cast], but after conversion functions are mentioned.

According to my reading of the DWP the compiler should prefer a
static_cast over a reinterpret_cast , but I am not sure whether this
means it should ignore a "close match" static_cast which would be in
error.

Help!

Bryan Boreham, Front Office Development, Credit Suisse Financial Products
1 Cabot Square, London E14 4QJ, phone: +44 171 516 2058, fax:+44 171 516 2688
email: bboreham@csfp.co.uk

[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <9509121036.AA22484@liverpool.csfp.co.uk>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
From: bboreham@csfp.co.uk (Bryan Boreham)
Newsgroups: comp.std.c++
Subject: User-defined reference casts
Date: 12 Sep 1995 14:49:29 GMT
Organization: ?
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <9509121036.AA22484@liverpool.csfp.co.uk>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
Content-Length: 1527
X-Lines: 47
Originator: clamage@taumet