tags:

views:

1076

answers:

5

What is the best way to indicate that an object wants to take ownership of another object? So far, I've been using a std::auto_ptr in the public interface, so the client knows that the interface wants to take ownership of the passed object.

However, the latest GCC tells me auto_ptr is deprecated, so I wonder what is recommended? boost::interprocess::unique_pointer looks like a good candidate, but is this really the best solution out there?

A: 

std::auto_ptr implements ownership passing on copy and assignment, so there is nothing special you should do about it:

std::auto_ptr< T > p = somePtr; // not p owns object, referenced by somePtr
std::auto_ptr< T > q = myObj.GetAutoPtr(); // not q owns object referenced by auto_ptr in myObj

But passing object ownership is not a good design practice, it conducts to leaks and object lifetime relative errors.

Igor Semenov
Completely disagree with your conclusion. Ownership passing is well established and a good way of describing what your code does.
Martin York
We could agree saying that it can be a messy design practice, to use just when there are good reasons.
Blaisorblade
A: 

I don't remember std::auto_ptr being deprecated.
Anybody have a link to the appropriate standards meeting where they discuss this?

A quick google found this: http://objectmix.com/c/113487-std-auto_ptr-deprecated.html

>> In fact the latest publicly available draft lists auto_ptr in appendix
>> D, meaning that there is clear intent to formally deprecate it in the
>> next revision of C++. A new class named unique_ptr is going to provide
>> a replacement for auto_ptr, the main difference being that unique_ptr
>> uses rvalue-references instead of voodoo magic to properly achieve
>> move semantic.
>
> Is a reference implementation available? Is it too early to start using it?
>

In order to use unique_ptr you need first to have a compiler which
properly supports rvalue references. These can be hard to find nowadays,
as the feature has not yet been standardized, although the situation is
quickly improving. For example GCC has very recently added the feature
in v4.3 (http://gcc.gnu.org/gcc-4.3/cxx0x_status.html). If you are lucky
enough to have one of those compilers, most probably they already ship a
version of unique_ptr (it's a sort of benchmark for the feature, you
know). In any case, you can find reference implementations on the
internet by just googling unique_ptr.

So it looks like their are moves to deprecate auto_ptr in favor of unique_ptr (which has the same semantics). But it needs a compiler that supports the proposed new features in the upcoming version of C++.

But there is still another meeting and thus vote to come so things could change before the standard is made concrete.

Martin York
+8  A: 

boost::interprocess is a library for interprocess communication, so I wouldn't use it for different purposes.

As discussed on this forum:

http://objectmix.com/c/113487-std-auto_ptr-deprecated.html

std::auto_ptr will be declared deprecated in the next version of the standard, where it will be recommended the usage of std::unique_ptr, which requires rvalue references and move semantics to be implemented (that's a fairly complicated feature).

Until the new standard is released, I would simply try to disable the warning if possible, or ignore it, for maximum portability.

If you want to already switch to the next language standard, it is possible since rvalue references have been implemented (see http://russ.yanofsky.org/rref/), so also std::unique_ptr should be supported.

On of the advantages of the new semantics is that you can pass to the move constructor also a temporary or any rvalue; in other cases, this allows avoiding to copy (for instance) objects contained inside a std::vector (during reallocation) before destroying the original ones.

Blaisorblade
'Will be deprecated' is to strong. The current draft is proposing this change. Wait for it to become a standard.
Martin York
yeah. but hands down, it would be very surprising if auto_ptr wouldn't be deprecated. it serves the same purpose as unique_ptr, just in a horrible way :) (abusing the cctor)
Johannes Schaub - litb
@litb: Though I have come to respect auto_ptr, I agree with you it can be difficult to use correctly. But C++ is way to complex to just assume that unique_ptr is going to be an acceptable replacement without a lot of study and testing, at least auto_ptr's weaknesses are well understood.
Martin York
That said. I don't think it is a for gone conclusion that it will be replace. Also Even if it is deprecated auto_ptr will still be with us for a long time (back wards compatibility and all).
Martin York
+3  A: 

std::unique_ptr is indeed the new recommended way. With C++0x containers will become move-aware, meaning that they can handle types which are movable correctly (i.e., std::vector<std::auto_ptr<x> > does not work, but std::vector<std::unique_ptr<x>> will).

For boost, the boost::interprocess containers already support movable types, where boost::interprocess::unique_ptr is one of them. They resemble movable types in pre C++0x by using some of the "normal" boost-template wizardry, and use r-value references where they are supported.

I didn't know about the auto_ptr dedicated deprecation, though, but I've not followed the new standard evolution closely.

(edit) The implementation of boost::interprocess::unique_ptr is indeed not a "public" smart-pointer like boost::shared_ptr or boost::scoped_ptr, but it is (see boost.interprocess's site) not just for shared-memory, but can also be used for general-purpose.

However, I'm quite sure that if GCC deprecates the auto_ptr template, they already provide their own unique_ptr implementation (not much use to deprecate if you not have a viable alternative yet).

However, that all being said, if you're working on a C++0x platform, use unique_ptr, available from the compiler's lib, if not, stick with auto_ptr.

gimpf
Notice the question asks about boost::interprocess::unique_pointer (actually unique_ptr), which is a smart pointer for shared memory supported by that library.
Blaisorblade
Yes, I indeed ignored this more than I should have. Still, the boost.interprocess's unique_ptr implementation is general purpose, not just for interprocess's shared-memory. I hope the edit made this more clear, though I doubt it ever more...
gimpf
didn't the qustioner ask about why auto_ptr is deprecated. i think this excellent answers it
Johannes Schaub - litb
+1  A: 

I agree where possible you should use types where the compiler assists in ownership transfer.

Where you don't have that choice of data types and are passing raw pointers, I follow the Taligent programming guidelines of naming methods which relinquish ownership as orphanBlah and parameters which take ownership as adoptBlah.

Andy Dent