473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
Jul 19 '05 #1
4 3292
"Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote in message
news:bk**********@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
Jul 19 '05 #2
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" <dh***@codelogicconsulting.com> wrote in message
news:bk**********@news.astound.net...
"Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote in message
news:bk**********@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

Jul 19 '05 #3
"Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote in message
news:bk**********@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
Jul 19 '05 #4
On Sun, 21 Sep 2003 20:39:06 -0400, "Marcin Vorbrodt"
<mv*****@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
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: Jamie Burns | last post by:
Hello, I realise that I just dont get this, but I cannot see the need for auto_ptr. As far as I have read, it means that if you create an object using an auto_ptr, instead of a raw pointer, then...
3
by: Markus Dehmann | last post by:
The main difference between std::auto_ptr and boost::shared_ptr is that auto_ptr has the transfer-of-ownership semantic. That makes it impossible to store auto_ptrs in STL containers because the...
4
by: Rein Anders Apeland | last post by:
Consider the following working code: #include <memory> #include <iostream> void print_ptr( const std::auto_ptr< int > & thePtr = std::auto_ptr< int >() ) {
14
by: Andrew | last post by:
Hello all: After spending some time figuring out auto_ptr class' implementation, I decided to write a small article detailing its use of the auto_ptr_ref proxy class to enable construction and...
2
by: Stephan Hoffmann | last post by:
Hi, I'm new to std::auto_ptr<> and wanted to use it with a base class and several derived classes. I'm using gcc 3.3.5 and get a compile error I don't know how to resolve when compiling the...
2
by: flopbucket | last post by:
I saw the following post a few messages back and am trying to understand exactly why this is so. Thanks for the information. My comments begin with **. The original post was arguing that STL...
5
by: Raider | last post by:
Is it exception-safe to write like this: void foo(auto_ptr<Tx) { ... } void bar() { foo(auto_ptr<T>(new T(...));
9
by: dragoncoder | last post by:
Hi all, I am trying to understand the auto_ptr_ref role in the implementation of auto_ptr<>. I read the information on net but still not 100% sure of it. My plan is as follows. 1. To see the...
39
by: Andre Siqueira | last post by:
Hello all, I have a member function like thist: Query(const std::string & id, std::auto_ptr<Modifiermodif = std::auto_ptr<Modifier>()) when a try to instantiate a Query like ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.