Jump to content

  • Log In with Google      Sign In   
  • Create Account


Concerning std::auto_ptr


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Khatharr   Crossbones+   -  Reputation: 2818

Like
0Likes
Like

Posted 27 November 2012 - 06:27 PM

I find myself creating RAII templates for COM and for IrrKlang objects that behave in a very similar fashion to std::auto_ptr (because I intentionally model them after that behavior). The reason for this is that in both cases the objects manage lifetime using internal reference counting that triggers deconstruction by calls to a member function: comObj->Release() and irkObj->drop(), so std::auto_ptr's simple 'delete' won't cut it here.

The documentation I have says that std::auto_ptr is deprecated, so I'm guessing that we're supposed to use shared_ptr now, even for unshared pointers. I see that shared_ptr supports custom deletion with functors, but the syntax of it makes it sort of difficult to 'shoehorn' that into a new type. In other words, does anyone know of a way to make a derived type from shared_ptr that uses a custom deleter of a set type? So I could say something like:

[source lang="cpp"]//This does not work because template syntax is stupid:template<class T> class Releaser { void operator()(T* p) { p->Release(); }};template<class T> class Releasable : public shared_ptr<T, Releaser>;Releasable<foo> bar(fooPtr); //will call fooPtr->Release(); on dtor[/source]

Edited by Khatharr, 27 November 2012 - 06:28 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Sponsor:

#2 JTippetts   Moderators   -  Reputation: 8157

Like
0Likes
Like

Posted 27 November 2012 - 06:32 PM

If you don't need to share a pointer, you can use std::unique_ptr,

#3 Khatharr   Crossbones+   -  Reputation: 2818

Like
0Likes
Like

Posted 27 November 2012 - 06:38 PM

Not using C++11, sadly. Is that why they deprecated it?

Edited by Khatharr, 27 November 2012 - 06:39 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#4 JTippetts   Moderators   -  Reputation: 8157

Like
0Likes
Like

Posted 27 November 2012 - 06:41 PM

Yes.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS