auto_ptr and sink function

M

Marcin Vorbrodt

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
 
D

David B. Held

Marcin Vorbrodt said:
[...]
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
 
M

Marcin Vorbrodt

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 said:
Marcin Vorbrodt said:
[...]
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
 
D

David B. Held

Marcin Vorbrodt said:
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
 
T

tom_usenet

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top