Setting a boost shared_ptr to NULL

If you need to set a boost shared_ptr to NULL, then you can do it like this:

boost::shared_ptr();

So, if you have a function that returns a boost shared_ptr and you want to pass NULL to represent either an error state or a not-found state, you can do this:

#include 
typedef boost::shared_ptr OohClassPtr;
typedef std::map< const char*, OohClassPtr > OohClassMap;

OohClassPtr FindMyClass( const char* key, OohClassMap& ocmRef )
{
	OohClassPtr ptr;
	OohClassMap::iterator itr = ocmRef.find(key);
	if (itr != ocmRef.end())
		ptr = itr->second;
	else
		ptr = boost::shared_ptr();
	return ptr;
}

Handy!

Leave a Reply

Your email address will not be published. Required fields are marked *