From 2382959810669711168
X-Google-Thread: f78e5,1b57b95bf879a9da
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news1.google.com!news3.google.com!news.glorb.com!newsfeed.stueberl.de!newsfeed.vmunix.org!peer-uk.news.demon.net!kibo.news.demon.net!mutlu.news.demon.net!news.demon.co.uk!demon!stump.algebra.com!devnull
From: Michael.Karcher@writeme.com (Michael Karcher)
Newsgroups: comp.std.c++
Subject: Re: auto_ptr as a return value
Date: Thu, 25 Aug 2005 02:43:31 GMT
Organization: Freie Universitaet Berlin
Lines: 56
Sender: mail2news@demon.net
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <3n3kr7F19519cU1@uni-berlin.de>
References: <1124894045.583570.226570@g14g2000cwa.googlegroups.com>
NNTP-Posting-Host: news.news.demon.net
X-Trace: news.demon.co.uk 1124937829 25227 158.152.254.254 (25 Aug 2005 02:43:49 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Thu, 25 Aug 2005 02:43:49 +0000 (UTC)
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-User-Agent: tin/1.7.8-20050315 ("Scalpay") (UNIX) (Linux/2.6.10-ac12-nfsacl-smp (i686))
X-Orig-X-Trace: news.uni-berlin.de lAxo63wOX8pZg9zUccgRMwq8tbPrnJ56KN1tOOz0mgP4Ho
X-Virus-Scanned: amavisd-new at cs.mu.OZ.AU
X-Cancel-Lock: sha1:uwQ9PI1ZXfBm9YGNxRtmR0sS86M=
X-Received: (from fjh@localhost)
	by mulga.cs.mu.OZ.AU (8.12.10+Sun/8.12.9/Submit) id j7P2hVk5003077;
	Thu, 25 Aug 2005 12:43:31 +1000 (EST)
X-Path: comp-std-cpp-robomod!not-for-mail
X-Delivered-To: std-c++@ucar.edu
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Orig-Path: not-for-mail
X-Newsgroups: comp.std.c++
Xref: g2news1.google.com comp.std.c++:1918

andru123@hotmail.com wrote:
> Hi,
> 
> 
> auto_ptr<Bla> makeBla()
> {
>  return NULL;
> }
> 
> //Assuming auto_ptr is OWNER of the objects, I MUST call release()
> auto_ptr<Bla> bla = makeBla().release();

No, you do not need to call release to transfer ownership from one
auto_ptr to another auto_ptr. Assignment and copy construction (this
also applies to copy-construction for by-value-arguments, a common
auto_ptr pitfall) do _always_ transfer ownership. So you should just write

  auto_ptr<Bla> bla = makeBla();

or, equivalently

  auto_ptr<Bla> bla(makeBla());

> Can I do this then:
> 
> auto_ptr<Bla> bla = NULL;
> if (makeBla() != NULL)
> {
>  bla = makeBla();
> }
> 
> My question:
> will the result of the first call of makeBla() be deleted/ handled
> correctly?

You can do this, but the line inside the if construct does not call
release, so it works even if the result of makeBla is NULL. The result
of the first call will be handled correctly, as this is the point of
auto_ptr. If an auto_ptr is destroyed still having a pointee, it is
deleted. The first call creates a temporary auto_ptr object, where the
full-expression it is in is the condition of the if statement. After
evalutating this expression, all temporaries that are not bound to references
by that expression, are deleted.

Your example will of course *not* do what you intend if makeBla sometimes
returns NULL and sometimes a valid pointer depending on external state that
may change between the two calls.

Michael Karcher

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]



