Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smart pointer that does transfer of ownership and is not shared when C++11 is not an option

Currently I am solving my problem with boost::shared_ptr but the semantics is not quite right, since I am "transplanting" members from one object to another. I was looking through this list but it didn't yield too much. Same goes for my brief google searches.

Essentially I am looking for a unique_ptr implementation that works with my gcc4.2 (Hence the restriction to not use C++11)

like image 358
sebastiangeiger Avatar asked Jan 18 '26 09:01

sebastiangeiger


2 Answers

You could stick with std::auto_ptr until Boost implements unique_ptr on top of the new Boost Move library (C++03 compatible).

See this mailing list traffic d.d. November 10th 2011: http://boost.2283326.n4.nabble.com/smart-ptr-Inclusion-of-unique-ptr-td4021667.html

Edit And Boost Interprocess has a uniqe_ptr<> class template floating around:

  • http://www.boost.org/doc/libs/1_48_0/doc/html/boost/interprocess/unique_ptr.html
like image 110
sehe Avatar answered Jan 20 '26 21:01

sehe


Depending on what exactly you want, you might consider using boost::scoped_ptr. It is very similar to std::unique_ptr, but it cannot be moved (because C++03 doesn't know about move-semantics). It can, however be swapped. So, when you want to transfer ownership, just do this:

boost::scoped_ptr<T> dummy;
dummy.swap(my_ptr);
// now you have basically transferred ownership from my_ptr to dummy
like image 38
Björn Pollex Avatar answered Jan 20 '26 22:01

Björn Pollex