🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Bryan Boreham Tue, 12 Sep 1995 14:49
User-defined reference casts
#1951
Author: 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. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads