From 1972246624704461825
X-Google-Thread: f78e5,86930e54cbf7375c
X-Google-Attributes: gidf78e5,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news2.google.com!news.glorb.com!news.cs.univ-paris8.fr!proxad.net!feeder1-2.proxad.net!usenet-fr.net!grolier!newsfeed.vmunix.org!peer-uk.news.demon.net!kibo.news.demon.net!news.demon.co.uk!demon!stump.algebra.com!devnull
From: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Newsgroups: comp.std.c++
Subject: Re: operator class&() { return *this; } considered harmful?
Date: Fri,  2 Nov 2007 16:54:13 GMT
Organization: [Infostrada]
Lines: 73
Sender: mail2news@demon.net
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <TLCWi.161350$U01.1150349@twister1.libero.it>
References: <4726a73f$1@clarion.carno.net.au> <1193750417.817345.137430@v3g2000hsg.googlegroups.com> <4727abe3$1@clarion.carno.net.au> <pyOVi.156741$%k.298127@twister2.libero.it> <4727ef35$1@clarion.carno.net.au>
NNTP-Posting-Host: news.news.demon.net
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
X-Trace: news.demon.co.uk 1194022463 12770 158.152.254.254 (2 Nov 2007 16:54:23 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Fri, 2 Nov 2007 16:54:23 +0000 (UTC)
X-Original-To: std-c++@mailman.ucar.edu
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-User-Agent: Thunderbird 2.0.0.6 (Windows/20070728)
X-IronPort-Anti-Spam-Result: Ao8CAAuVKkfBRsBm/2dsb2JhbACQNQ
X-IronPort-Anti-Spam-Filtered: true
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
X-Path: comp-std-cpp-robomod!not-for-mail
X-Received: (from fjh@localhost)
	by mulga.csse.unimelb.edu.au (8.13.8+Sun/8.13.8/Submit) id lA2GsDMv012530;
	Sat, 3 Nov 2007 03:54:13 +1100 (EST)
X-NNTP-Posting-Date: Fri, 02 Nov 2007 11:14:11 MET
X-Delivered-To: std-c++@mailman.ucar.edu
X-Authentication-Warning: mulga.csse.unimelb.edu.au: fjh set sender to devnull@stump.algebra.com using -f
X-SMTP-Auth: no
X-Newsgroups: comp.std.c++
Xref: g2news2.google.com comp.std.c++:952

[Note to mods: this is a repost. Although the previous post had been
approved, it somehow didn't appear on the newsgroup.]

Michael Norrish ha scritto:
> > Alberto Ganesh Barbati wrote:
>> >> Michael Norrish ha scritto:
>>> >>> Clearly, the rvalue reference proposal solves all sorts of other
>>> >>> interesting issues apart from the one in my example, but my question
>>> >>> is really whether or not
>>> >>>
>>> >>>   operator C&() { return *this; }
>>> >>>
>>> >>> will safely achieve what I want.
>>> >>>
>> >>
>> >> I don't think so. According to my interpretation, 12.2/5 doesn't apply
>> >> here, because in line:
>> >>
>> >>   C &x = f();
>> >>
>> >> the reference x is *not* bound to a temporary, but rather to the
result
>> >> of the call to operator C& which is a reference (such reference
>> >> *happens* to be bound to a temporary but this fact is irrelevant to a
>> >> strict interpretation of 12.2/5).
>> >>
>> >> I expect the temporary to be properly destroyed at the end of the
>> >> full-expression, making x a dangling reference.
> >
> > If this is so, is there any point to the language in 8.5.3/5 allowing
> > this?  (This language is "if the initializer expression ... has a
> > class type ... and can be implicitly converted to an lvalue....".  The
> > footnote emphasises that this situation can only arise when you have a
> > conversion function returning a reference type.)

Notice that in this case "the reference is bound to the lvalue result of
the conversion" confirming my interpretation.

> > That clause only applies when the initialisor is not an lvalue, so
> > there will be a temporary here anyway.  That temporary can only get
> > turned into an l-value via a conversion function, but if you are
> > right, then the conversion function can't usefully return a reference
> > to the temporary it's called on because that temporary will disappear
> > as soon as the declaration of the reference completes.  (The
> > conversion function could return a reference to something other than
> > the temporary of course; some global perhaps).

The fault in your reasoning is that it's not true that the initializer
must be an temporary to trigger this case. Consider this:

  class B;

  class A
  {
  public:
    operator B& ();
  };

  A a;
  B& b = a;

In fact, this example shows a very good reason why this case is allowed.

HTH,

Ganesh

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]



