2

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)

Community
  • 1
  • 1
sebastiangeiger
  • 3,386
  • 7
  • 29
  • 37
  • strongly suggest this: http://stackoverflow.com/questions/569775/smart-pointers-boost-explained – sehe Dec 01 '11 at 15:15
  • The latest boost (1.48) has a Boost.Move library that mimics move semantics in C++03. I guess that a `unique_ptr` could be implemented on top of it (and maybe it was, I haven't looked at it much). – R. Martinho Fernandes Dec 01 '11 at 15:16
  • @sehe Saw it, the pointers are either shared or can't transfer the ownership at all (scoped) – sebastiangeiger Dec 01 '11 at 15:19
  • ANY ownership pointer will not work with the STL library. That is why everyone tells you to stay away from auto_ptr. There are no serious bugs; ownership pointers will inherently not work with the STL library. – djhaskin987 Dec 01 '11 at 15:24
  • Ok, I think so far I can live with not being STL compatible. The pointer is well hidden in one class and never exposed to anyone. – sebastiangeiger Dec 01 '11 at 15:26

6 Answers6

7

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:

sehe
  • 374,641
  • 47
  • 450
  • 633
3

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
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
1

Use std::auto_ptr<..>, it has exactly what you need.

Simon
  • 1,496
  • 8
  • 11
  • Everyone is telling me to stay away from `auto_ptr`. – sebastiangeiger Dec 01 '11 at 15:20
  • 1
    Be aware that using auto_ptr in STL containers may cause undefined behavior, depending on what you are doing. (That is partly why auto_ptr is being replaced by unique_ptr.) See http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=400 – Kevin Hopps Dec 01 '11 at 15:24
  • 1
    `auto_ptr` is deprecated in current C++ standard. Also in C++03 using it wasn't good practice. – Abyx Dec 01 '11 at 15:25
  • What are their reasons? I'm interested as I'm using auto_ptr as a placeholder for unique_ptr functionality as well. – Dennis Dec 01 '11 at 15:26
  • @Abyx, after saying something isn't good practice, it would be helpful and educational to explain why. – Kevin Hopps Dec 01 '11 at 15:46
1

llvm::OwningPtr - it has take method to take ownership.

Abyx
  • 12,345
  • 5
  • 44
  • 76
0

boost::shared_ptr with custom deleter.

This is something I used in unit test to simulate Corba _var class behaviour.

struct CondDel {
  bool doDel;
  CondDel() : doDel(true) {
  }
  template<typename T>
  void operator()(T* p) {
    if (doDel)
      delete p;
  }
};
class MyCorbaObj_var : public boost::shared_ptr<_objref_MyCorbaObj> {
public:
  MyCorbaObj_var(_objref_MyCorbaObj* p) : boost::shared_ptr<_objref_MyCorbaObj>(p, CondDel()) {
  }
  _objref_MyCorbaObj* _retn() {
    CondDel* cd = (CondDel*)_internal_get_deleter(typeid(CondDel));
    cd->doDel = false;
    return get();
  }
};
bond
  • 1
  • 4
-1

If you want copying objects of your class to make new copies of the pointed-to items, then no kind of pointer (auto_ptr, shared_ptr or naked pointer) will do what you want without some extra work on your part. You will need to manage the copying yourself. Be sure you implement a copy constructor and assignment operator so that they clone the pointed-to items and store the new pointers in the destination object. If you do this, auto_ptr and shared_ptr will both work just fine for you.

Kevin Hopps
  • 707
  • 3
  • 10
  • This is not what is being asked; anyway, a "pointer" that copies the pointed-to object is not a pointer any more, it's a **container**. – curiousguy Dec 06 '11 at 18:30