| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Marcin Vorbrodt" |
| Date: |
21 Sep 2003 07:39:06 PM |
| Object: |
auto_ptr and sink function |
A source function is a one that returns an auto_ptr object. A sink function
is one that takes auto_ptr as a parameter. So:
void Source(auto_ptr<SomeClass> aptr);
This will transfer the ownership correctly. How about this:
void Source(auto_ptr<SomeClass> & aptr);
Passing it as reference (non-const) should still do the trick, right?
Is there an advantage to doing so? Speed-wise I mean?
Martin
.
|
|
| User: "David B. Held" |
|
| Title: Re: auto_ptr and sink function |
21 Sep 2003 09:04:59 PM |
|
|
"Marcin Vorbrodt" <mvorbro@eos.ncsu.edu> wrote in message
news:bklgad$qir$1@uni00nw.unity.ncsu.edu...
[...]
This will transfer the ownership correctly. How about this:
void Source(auto_ptr<SomeClass> & aptr);
Passing it as reference (non-const) should still do the trick,
right? Is there an advantage to doing so? Speed-wise I
mean?
For suitable definitions of "trick", sure. But note that it
doesn't call the "move c'tor", so ownership is not transferred.
Dave
.
|
|
|
| User: "Marcin Vorbrodt" |
|
| Title: Re: auto_ptr and sink function |
21 Sep 2003 09:17:36 PM |
|
|
what if that reference is then assigned to auto-pointer like this:
class C {
public:
C(auto_ptr<X> & ptr) : _ptr(ptr) {}
private:
auto_ptr<X> _ptr;
};
Will that transfer the ownership correctly?
"David B. Held" <dheld@codelogicconsulting.com> wrote in message
news:bkllf8$iom$1@news.astound.net...
"Marcin Vorbrodt" <mvorbro@eos.ncsu.edu> wrote in message
news:bklgad$qir$1@uni00nw.unity.ncsu.edu...
[...]
This will transfer the ownership correctly. How about this:
void Source(auto_ptr<SomeClass> & aptr);
Passing it as reference (non-const) should still do the trick,
right? Is there an advantage to doing so? Speed-wise I
mean?
For suitable definitions of "trick", sure. But note that it
doesn't call the "move c'tor", so ownership is not transferred.
Dave
.
|
|
|
| User: "David B. Held" |
|
| Title: Re: auto_ptr and sink function |
21 Sep 2003 09:56:31 PM |
|
|
"Marcin Vorbrodt" <mvorbro@eos.ncsu.edu> wrote in message
news:bklm32$sub$1@uni00nw.unity.ncsu.edu...
what if that reference is then assigned to auto-pointer like this:
class C {
public:
C(auto_ptr<X> & ptr) : _ptr(ptr) {}
private:
auto_ptr<X> _ptr;
};
Will that transfer the ownership correctly?
Since you now have a different instance of auto_ptr<X>,
yes, the copy [move] c'tor will be called and ownership
transferred.
Dave
.
|
|
|
|
|
|
| User: "tom_usenet" |
|
| Title: Re: auto_ptr and sink function |
22 Sep 2003 03:42:51 AM |
|
|
On Sun, 21 Sep 2003 20:39:06 -0400, "Marcin Vorbrodt"
<mvorbro@eos.ncsu.edu> wrote:
A source function is a one that returns an auto_ptr object. A sink function
is one that takes auto_ptr as a parameter. So:
void Source(auto_ptr<SomeClass> aptr);
This will transfer the ownership correctly. How about this:
void Source(auto_ptr<SomeClass> & aptr);
Passing it as reference (non-const) should still do the trick, right?
Is there an advantage to doing so? Speed-wise I mean?
It is probably marginally faster (depending on optimization settings),
but it is no longer explicit. e.g.
void Sink(auto_ptr<SomeClass> & aptr)
{
aptr.reset(new SomeClass);
//actually a source!
}
This obviously isn't possible if you pass by value, so the parameter
is a proper sink. In addition, you can pass temporary auto_ptrs by
value. e.g.
//illegal for reference version:
Sink(auto_ptr<SomeClass>(new SomeClass));
Tom
.
|
|
|
|
| User: "Marcin Vorbrodt" |
|
| Title: Re: auto_ptr and sink function |
21 Sep 2003 07:39:49 PM |
|
|
s/Source/Sink/g
"Marcin Vorbrodt" <mvorbro@eos.ncsu.edu> wrote in message
news:bklgad$qir$1@uni00nw.unity.ncsu.edu...
A source function is a one that returns an auto_ptr object. A sink
function
is one that takes auto_ptr as a parameter. So:
void Source(auto_ptr<SomeClass> aptr);
This will transfer the ownership correctly. How about this:
void Source(auto_ptr<SomeClass> & aptr);
Passing it as reference (non-const) should still do the trick, right?
Is there an advantage to doing so? Speed-wise I mean?
Martin
.
|
|
|
|

|
Related Articles |
|
|