On Sat, 1 Dec 2007 11:36:51 -0800 (PST) in comp.lang.c++,
How about
if (retvalue == boost::shared_ptr<sometype>()) {
How about:
if ( retvalue == NULL ) { ... }
I've not looked at boost::shared_ptr in this regard, but all of
the intelligent pointers I've written accept it.
In practice, if I think there's the slightest chance of using
intelligent pointers, I'll write something like:
template< typename T >
bool
isValid( T const* p )
{
return p != NULL ;
}
and use it. Adding the necessary overloads for the intelligent
pointers as needed. (This allows easily switching back, when
you realize that the smart pointer doesn't work.) But as I
said, a correctly designed intelligent pointer should support
all of the idioms a raw pointer does: "if( ptr == NULL )", and
even "if ( ptr )" (although I've never seen a coding guideline
that would allow use of the latter).
And if worse comes to worse, there's always:
if ( ptr.get() == NULL ) ...
Yes, I tryed if( retval == NULL ) and it would not compile. However, the
get method does work. I've got it now, I just don't like it much over using
raw pointers. I've always been of the opinion that, if I make a method that
allocates something, I just document it and count on the caller to know what
to do. I don't think I like trying to idiot proof things, because I am just
introducing more nuiances. If I've never used these smart pointers and don't
know how, I suspect the callers of my methods won't either. I really doubt
they will take the time to learn how the shared_ptr works, what can and
can't be done with it etc., no matter how much I do or don't. Ah well.