Boost smart pointer - scoped_ptr

Keywords: C++ Back-end

boost::scoped_ptr and std::auto_ptr is very similar. It is a simple smart pointer, which can ensure that the object is automatically released after leaving the scope. The following code demonstrates the basic application of this pointer:

#include <string>
#include <iostream>
#include <boost/scoped_ptr.hpp>

class implementation
    ~implementation() { std::cout <<"destroying implementation\n"; }
    void do_something() { std::cout << "did something\n"; }

void test()
    boost::scoped_ptr<implementation> impl(new implementation());

void main()
    std::cout<<"Test Begin ... \n";
    std::cout<<"Test End.\n";

The output of this code is:

Test Begin ...
did something
destroying implementation
Test End.


You can see that when the implementation class leaves the impl scope, it will be deleted automatically, so as to avoid memory leakage caused by forgetting to call delete manually.

boost::scoped_ptr features:

boost::scoped_ Implementation of PTR and std::auto_ptr is very similar. It uses an object on the stack to manage an object on the heap, so that the objects on the heap are automatically deleted when the objects on the stack are destroyed. The difference is that boost::scoped_ptr has a stricter use restriction - it cannot be copied. This means: boost::scoped_ptr pointers cannot convert their ownership.

  1. Ownership cannot be converted
    boost::scoped_ The object life cycle managed by PTR is limited to an interval (between "{}" where the pointer is located) and cannot be transferred outside the interval, which means boost::scoped_ptr object cannot be used as the return value of a function (std::auto_ptr can).

  2. Cannot share ownership
    This is similar to std::auto_ptr is similar. On the one hand, this feature makes the pointer simple and easy to use. On the other hand, it also causes the weakness of function - it can not be used in stl containers.

  3. Cannot be used to manage array objects
    Because boost::scoped_ptr deletes managed objects through delete, while array objects must be deleted through deletep [], so boost::scoped_ptr cannot manage array objects. If you want to manage array objects, you need to use boost::scoped_array class.

boost::scoped_ Common operations of PTR:

It can be simplified as follows:

namespace boost {

    template<typename T> class scoped_ptr : noncopyable {
        explicit scoped_ptr(T* p = 0);

        void reset(T* p = 0);

        T& operator*() const;
        T* operator->() const;
        T* get() const;

        void swap(scoped_ptr& b);

    template<typename T>
    void swap(scoped_ptr<T> & a, scoped_ptr<T> & b);

Its common operations are as follows:

Member function



Members that access managed objects as references


Members that access managed objects as pointers


Release the managed object and manage another object

swap(scoped_ptr& b)

Swap two boost::scoped_ptr managed objects


The following test code demonstrates the basic use of these function functions.

#include <string>
#include <iostream>

#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>

#include <boost/config.hpp>
#include <boost/detail/lightweight_test.hpp>

void test()
    // test scoped_ptr with a built-in type
    long * lp = new long;
    boost::scoped_ptr<long> sp ( lp );
    BOOST_TEST( sp.get() == lp );
    BOOST_TEST( lp == sp.get() );
    BOOST_TEST( &*sp == lp );

    *sp = 1234568901L;
    BOOST_TEST( *sp == 1234568901L );
    BOOST_TEST( *lp == 1234568901L );

    long * lp2 = new long;
    boost::scoped_ptr<long> sp2 ( lp2 );

    BOOST_TEST( sp.get() == lp2 );
    BOOST_TEST( sp2.get() == lp );

    BOOST_TEST( sp.get() == NULL );


void main()

boost::scoped_ptr and STD:: Auto_ Selection of PTR:

boost::scoped_ptr and STD:: Auto_ The functions and operations of PTR are very similar. How to select between them depends on whether the ownership of the managed object needs to be transferred (such as whether it needs to be used as the return value of the function). If you don't need this, you can use boost::scoped_ptr, let the compiler check more strictly to find some incorrect assignment operations.

Posted by rsassine on Sat, 30 Oct 2021 21:46:14 -0700