What are you using for smart pointers these days?

This topic is 4891 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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 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 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 on other sites
I use C#.NET for my smart pointers ;) Nah, really. Just rolled my own for the unmanaged work.

~Graham

Share on other sites
What's this 'pointer' you speak of?
...
Anyway, I don't use low-level languages like C++ anymore; generally, I just use python.

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 on other sites
I'm a big fan of Loki's SmartPtr, i like the policy-based approach to its design, so I can easily adapt it to what I need (new ownership policies, new checking policies, etc)

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 on other sites
Quote:
 Original post by TelastynBecuase 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...

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 10
• 13
• 20
• Forum Statistics

• Total Topics
632948
• Total Posts
3009393
• Who's Online (See full list)

There are no registered users currently online

×