From 5348192069391090820
X-Google-Thread: f78e5,86930e54cbf7375c
X-Google-Attributes: gidf78e5,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news1.google.com!news.glorb.com!peer1.news.newnet.co.uk!194.159.246.34.MISMATCH!peer-uk.news.demon.net!kibo.news.demon.net!news.demon.co.uk!demon!stump.algebra.com!devnull
From: greghe@mac.com (Greg Herlihy)
Newsgroups: comp.std.c++
Subject: Re: operator class&() { return *this; } considered harmful?
Date: Fri,  2 Nov 2007 05:33:54 GMT
Organization: Sonic.Net
Lines: 54
Sender: mail2news@demon.net
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <C34FB26D.E25%greghe@mac.com>
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="US-ASCII"
Content-Transfer-Encoding: 7bit
X-Trace: news.demon.co.uk 1193981656 29414 158.152.254.254 (2 Nov 2007 05:34:16 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Fri, 2 Nov 2007 05:34:16 +0000 (UTC)
X-Thread-Topic: operator class&() { return *this; } considered harmful?
X-Original-To: std-c++@mailman.ucar.edu
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-User-Agent: Microsoft-Entourage/11.3.6.070618
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
X-Thread-Index: Acgc4Nh3FxqMPojUEdyyeAAWy5Ss6w==
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 lA25XsWM017694;
	Fri, 2 Nov 2007 16:33:54 +1100 (EST)
X-NNTP-Posting-Date: 01 Nov 2007 23:42:24 GMT
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++:948




On 10/30/07 11:18 PM, in article 4727ef35$1@clarion.carno.net.au, "Michael
Norrish" <michael.norrish@nicta.com.au> wrote:

> 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.)

There is little point in defining a conversion to a reference of the same
class type - because an object can always be used to initialize a reference
of its own type. In other words, adding the C&() operator has no real effect
on the program (except to make it more difficult for the compiler to warn
that the function f() is returning a reference to a de-allocated object.)

Moreover, having f() return a C object by value would not only be safe, but
would also be slightly more efficient than the current implementation of f()
(owing to the Named Return Value Optimization).

Greg

---
[ 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                      ]



