Boost scoped_ptr vs unique_ptr

By , last updated March 27, 2016

A unique_ptr is also known as std::unique_ptr. Before C++11 it was boost::unique_ptr, but became a part of STD library in C++11 and is now called std::unique_ptr.

A scoped_ptr is generally known as boost::scoped_ptr, and is from the “ancient” ages before C++11 came along with the <memory> header with std::unique_ptr (and std::shared_ptr). Note that there is NO such thing as std::scoped_ptr! Only boost::scoped_ptr and std::unique_ptr. Generally, its std::unique_ptr you should use.

If you’ve been using boost, and have any scoped_ptr lying around, you can (almost always) safely replace them with std::unique_ptr. There are a few subtle differences between scoped_ptr and unique_ptr, but they are mostly an artifact on how “old” they are.

boost::scoped_ptr const std::unique_ptr
Copyable No No
 Movable  No  Yes

Related: Memory leak with scoped_ptr

If you want to disallow moving with std::unique_ptr, use const std::unique_ptr.

boost::scoped_ptr const std::unique_ptr
Copyable No No
 Movable  No  No

Professional Software Developer, doing mostly C++. Connect with Kent on Twitter.


Be the first to comment.

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>