14

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?

5 Answers 5

9

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.

4
  • 'Will be deprecated' is to strong. The current draft is proposing this change. Wait for it to become a standard. Jan 13, 2009 at 9:58
  • 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) Jan 13, 2009 at 12:51
  • @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. Jan 14, 2009 at 7:31
  • 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). Jan 14, 2009 at 7:33
3

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.

3
  • Notice the question asks about boost::interprocess::unique_pointer (actually unique_ptr), which is a smart pointer for shared memory supported by that library. Jan 13, 2009 at 10:00
  • 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
    Jan 13, 2009 at 11:03
  • didn't the qustioner ask about why auto_ptr is deprecated. i think this excellent answers it Jan 13, 2009 at 12:48
2

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.

0

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.

2
  • 2
    Completely disagree with your conclusion. Ownership passing is well established and a good way of describing what your code does. Jan 13, 2009 at 9:45
  • We could agree saying that it can be a messy design practice, to use just when there are good reasons. Jan 13, 2009 at 9:52
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.