Unique_ptr boost equivalent?

Is there an equivalent class for C ++ 1x std :: unique_ptr in boost libraries? The behavior I'm looking for can have an exception-safe factory function, for example ...

std::unique_ptr<Base> create_base() { return std::unique_ptr<Base>(new Derived); } void some_other_function() { std::unique_ptr<Base> b = create_base(); // Do some stuff with b that may or may not throw an exception... // Now b is destructed automagically. } 

EDIT: Right now, I'm using this hack, which seems to be the best I can get at this moment ...

 Base* create_base() { return new Derived; } void some_other_function() { boost::scoped_ptr<Base> b = create_base(); // Do some stuff with b that may or may not throw an exception... // Now b is deleted automagically. } 
+53
c ++ boost c ++ 11 unique-ptr
Jun 01 '10 at 21:37
source share
5 answers

It is not possible to create something like unique_ptr without C ++ 0x (where it is part of the standard library, and therefore Boost does not need to be provided).

In particular, without references to rvalue, which are a feature in C ++ 0x, a reliable implementation of unique_ptr not possible with or without Boost.

There are several possible alternatives in C ++ 03, although each has its own drawbacks.

  • boost::shared_ptr is probably the easiest replacement in terms of features. You can safely use it wherever you would otherwise use unique_ptr , and it will work. This would not be so effective because added reference counting. But if you're looking for a simple replacement that can handle everything unique_ptr can do, this is probably the best choice. (Of course, shared_ptr can do a lot more, but it can also just be used as a replacement for unique_ptr .)
  • boost::scoped_ptr is similar to unique_ptr , but does not allow transfer of ownership. It works great while a smart pointer is designed to preserve exclusive ownership throughout its life.
  • std::auto_ptr works very similar to unique_ptr , but has several limitations, mainly that it cannot be stored in standard library containers. If you are just looking for a pointer that allows you to transfer ownership, but which is not intended to be stored in containers or copied, this is probably a good bet.
+66
Jun 01 '10 at
source share

Starting with Boost 1.57, there is an official implementation of unique_ptr in the Boost.Move library.

From the documentation :

(...) plugin replacement for std :: unique_ptr, also used from C ++ 03 Compilers.

The code is available in the header file <boost/move/unique_ptr.hpp> and is located in the boost::movelib . In addition, the Boost.Move library provides the factory function make_unique() in <boost/move/make_unique.hpp> , also in the boost::movelib .

Therefore, the example from the question can be implemented as follows:

 #include <boost/move/unique_ptr.hpp> using boost::movelib::unique_ptr; unique_ptr<Base> create_base() { return unique_ptr<Base>(new Derived); } 

See a live example on Wandbox . Note that the code compiles fine with gcc 4.6.4 in C ++ 98 mode (!).

What is interesting in boost::movelib::unique_ptr as applied to your case with base / derived classes, the implementation provides compile-time checking to declare a virtual destructor in the base class. If you skip it , the code will not compile (click the "Run (...)" button to see the compiler error message).

One small problem is that this includes files from the boost/move directory, but the code is in the boost::movelib (a little difference, but it can be annoying).

See also the topic in the expanded newsletter for more information.

Thanks to Ion Gaztanyaga for this absolutely unique and useful piece of code.

+32
Jan 28 '15 at 13:05
source share

You might want to try the proof of concept unique_ptr<> for Howard Hinnant for C ++ 03 (disclaimer - I don't know):

One of his examples returns unique_ptr<int> :

 unique_ptr<int> factory(int i) { return unique_ptr<int>(new int(i)); } 
+10
Jun 01 '10 at
source share

What about unique_ptr from interprocess library?

+5
Jun 01 '10 at 21:39
source share

I used Howard Hinnant unique_ptr . If you are not very good at reading crazy metaprogramming errors from your compiler, you might want to clearly understand. However, it acts as unique_ptr in 90% of cases.

Otherwise, I would suggest passing parameters as boost::scoped_ptr& and exchanging internally to steal the property. To get the return values โ€‹โ€‹of the unique_ptr style, use auto_ptr . Grab the return value of auto_ptr in shared_ptr or scoped_ptr to avoid using auto_ptr directly.

+4
Jun 02 2018-10-06T00:
source share



All Articles