C++0x unique_ptr replaces scoped_ptr taking ownership?

c++, c++11, smart-pointers

Solution

I've upvoted comonad's answer, but with a caveat:

Whenever you want to explicitely disallow move semantics, use a scoped_ptr const unique_ptr.

I have not come across any use cases where a `const std::unique_ptr` is inferior to a `boost::scoped_ptr`. However I'm open to education on the subject.

Edit:

Here is a use case of `boost::scoped_ptr` that I think should fail, but does not. It does fail for `std::unique_ptr`:

#include <iostream>

#ifdef USE_UNIQUEPTR

#include <memory>
typedef std::unique_ptr<int> P;

#else  // USE_UNIQUEPTR

#include <boost/scoped_ptr.hpp>
typedef boost::scoped_ptr<int> P;

#endif  // USE_UNIQUEPTR

int main()
{
    P p1(new int(1));
    {
        // new scope
#ifdef USE_UNIQUEPTR
        const P p2(new int(2));
#else  // USE_UNIQUEPTR
        P p2(new int(2));
#endif  // USE_UNIQUEPTR
        swap(p1, p2);  // should fail!
    }
    std::cout << *p1 << '\n';
}

If the promise of `boost::scoped_ptr` is that its resource will not escape the current scope, then it is not as good at holding that promise as a `const std::unique_ptr`. If we want to compare const boost::scoped_ptr to const::std::unique_ptr, I have to ask: for what purpose? They seem the same to me, except that a const std::unique_ptr allows customized construction and destruction.

Problem

I used to write code like this: ``` class P {}; class Q: public P {}; class A { // takes ownership A(P* p): p_(p) {} scoped_ptr<P> p_; }; A a(new Q); ``` With C++0x, should I rewrite class A as: ``` class A { // takes ownership A(unique_ptr<P>&& p): p_(p) {} unique_ptr<P> p_; }; ```

Original source

Related problems