Sign in to follow this  
dxben

What are you using for smart pointers these days?

Recommended Posts

As you travel around the open source world, and with the stl, boost, MS, etc., there are so many flavors of smart pointers. I am interested in hearing people's recommendations for usage, and what specific implementations you rely on.

Share this post


Link to post
Share on other sites
I don't use any premade ones, but I'm creating something very close to boost's shared_ptr (and weak_ptr) for my garbage collector. I wasn't sure how to efficiently invalidate weak pointers when an object is deleted, but the way they do it works perfectly for my system.

I'm going to use this system to create a custom scripting language, and then use that in some other projects I've been putting off for a long time.

But before I finish the above task, I'm going to finish reading the 3 design books I just ordered.

Share this post


Link to post
Share on other sites
I have rolled my own shared pointer, based on Boost (plays nicely with intellisense). I also have an pointer-like-thing, similar to an intrusive pointer, for handling my resource system (for doing things like loading and unloading resources as required, etc).

Share this post


Link to post
Share on other sites
boost::smart_ptr, and the very occasional std::auto_ptr. Also, I've invented (but have yet to use) industry::cloning_ptr, which deep-copys the pointed-to value polymorphically (which also means that cloning_ptr is forced to be slightly intrusive).

Example (from my use case):

#include <industry/provisional/cloning_ptr.hh>
using namespace industry;

#include <cstdlib>
#include <iostream>
using namespace std;


struct animal {
virtual void noise( void ) const = 0;
virtual animal * clone( allocator_interface & allocator ) const = 0;
};

struct feline : animal {
virtual feline * clone( allocator_interface & allocator ) const = 0;
};

struct cat : feline {
virtual void noise( void ) const {
cout << "Meow" << endl;
}
virtual cat * clone( allocator_interface & allocator ) const {
return new (allocator.allocate< cat >( 1 )) cat( *this );
}
};

struct dog : animal {
virtual void noise( void ) const {
cout << "Rowf" << endl;
}
virtual dog * clone( allocator_interface & allocator ) const {
return new (allocator.allocate< dog >( 1 )) dog( *this );
}
};

int main ( void ) {
cat cat;
cloning_ptr< feline > cat_1 = cat ; //creates a copy of cat
cloning_ptr< animal > cat_2 = cat_1 ; //(downcast) creates a copy of cat
//cloning_ptr< feline > cat_3 = cat_2; //(implicit upcast, should create a compile error) would create a copy of cat.
}




I consider it unfinished (hence the "industry/provisional" folder) due to the way allocator_interface is ugly, and incompatable with the standard allocators, thanks (entirely?) due to the fact that one simply can't have virtual templates. Eventually I'll probably code up some adaptors, but they'll most likely have to take *_allocator< char >s rather than *_allocator< type >s due to the varying sizes of the potentially polymorphic value held. There's also the fact that I havn't added *_cast<> wrappers that will work with it.

Work is currently paused on it, however, in part thanks to the fact that I froget what I was going to use it for. C'est la vie...

Share this post


Link to post
Share on other sites
Has anyone tried playing with Andrei Alexandrescu's implementation from his book (Modern C++ Design, AKA "the book with code on crack")? If so, how does it compare to the stl version?

Share this post


Link to post
Share on other sites
Becuase I've yet to get boost to compile nicely, and I've little direct use for auto_ptr, I've a few places where I've implimented something similar to [but less clean than] MaulingMonkey's example. I use it mainly these days for a common interface functor class I wrote so that it plays nice in containers.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Becuase I've yet to get boost to compile nicely,

Make sure you use bjam - using the makefiles yourself is a pain, using bjam I got everything to work first try with no hassle (just followed the instructions in "getting started").
Quote:
and I've little direct use for auto_ptr, I've a few places where I've implimented something similar to [but less clean than] MaulingMonkey's example.


You should see all the support code I've got for it. Clean on the outside, but a horrible mess on the inside I assure you :-). I even pulled in <boost/function.hpp> so I could implement operator->* so I could implement industry::mem_fun_ptr which is just like std::mem_fun only it works with anything that's got ->* defined... not only that, but I have no less than 6 overloads just for ->* at the moment, and that dosn't even allow me to use a two-argument functions with it...

Share this post


Link to post
Share on other sites
It should be noted that two smart pointers are currently being reviewed to become part of the C++ standard library, boost::shared_ptr and boost::weak_ptr, its in technical report 1 (TR1), std::tr1::shared/weak_ptr some compilers already implement most of TR1 you can find out the other goodies in the latest draft paper, note mem_fn is in there too cause mem_fun/mem_fun_ref are limited and can't handle smart pointers.

No doubt TR2 is round the corner then i'm guessing 2007/8 will be the year of the next standard revision (last being 2003 TC1) or C++0x by the looks of things.

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
Quote:
Original post by Telastyn
Becuase I've yet to get boost to compile nicely,

Make sure you use bjam - using the makefiles yourself is a pain, using bjam I got everything to work first try with no hassle (just followed the instructions in "getting started").
Quote:
and I've little direct use for auto_ptr, I've a few places where I've implimented something similar to [but less clean than] MaulingMonkey's example.


You should see all the support code I've got for it. Clean on the outside, but a horrible mess on the inside I assure you :-). I even pulled in <boost/function.hpp> so I could implement operator->* so I could implement industry::mem_fun_ptr which is just like std::mem_fun only it works with anything that's got ->* defined... not only that, but I have no less than 6 overloads just for ->* at the moment, and that dosn't even allow me to use a two-argument functions with it...


Ah, I did not mean less clean, I meant... erm, less good. Heh. The actual implimentation isn't so much a smart_pointer implimentation as a quick hack to make a pure virtual class pointer die at end of scope and not be totally broken when placed into containers.

One of these days I'll learn not to make those quick hacks...

And for Boost, I was actually using my OS's supplied package. Example code just didn't compile nicely... [I made a post elsewhere, which recieved no answer; something regarding templates being of the wrong export type....] I tried using bjam on my windows machine [a while ago], and bjam wouldn't even install.

Nothing from google, the faq, the install documentation, or in the one case my post here really helped, and I'm not nearly skilled enough to muck through makefiles or template magickry to make it work. *shrug* I figure one of these times it'll work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this