Is it correct to return null shared_ptr?

John Lock picture John Lock · May 15, 2016 · Viewed 31.3k times · Source

For example, there is a function that finds an object and returns shared_ptr if object is found, and must indicate somehow that no object was found.

std::vector<std::shared_ptr> Storage::objects;

std::shared_ptr<Object> Storage::findObject()
{
  if (objects.find)
  {
    return objects[x];
  }
  else
  {
    return nullptr;
  }
}

std::shared_ptr<Object> obj = Storage::findObject();
if (obj)
{
  print("found");
}
else
{
  print("not found");
}
  1. Is it correct to return shared_ptr implicitly initialized with nullptr like in upper example? It will work, but can be it done this way? Or should I return shared_ptr default constructed instead?

  2. What in case it would be weak_ptr? What is proper way to check that empty weak_ptr has been returned? by weak_ptr::expired function or are there other ways? If checking by weak_ptr::expired is the only way then how can I distinguish that function returned empty pointer, or object was just deleted(multi-thread environment)?

Answer

anton_rh picture anton_rh · May 15, 2016

Is it correct to return shared_ptr implicitly initialized with nullptr like in upper example?

Yes, it is correct to initialize shared_ptr with nullptr. It is also correct to assign nullptr to shared_ptr.

Or should I return shared_ptr default constructed instead?

You can do this in both ways:

  1. returning shared_ptr initialized with nullptr

    return shared_ptr<Object>(nullptr);
    
  2. returning shared_ptr default constructed.

    return shared_ptr<Object>();
    

Both ways are correct and both have the same effect. You can use whatever way you want.

What in case it would be weak_ptr? What is proper way to check that empty weak_ptr has been returned? by weak_ptr::expired function or are there other ways?

weak_ptr becomes nullptr (expires) whenever the last shared_ptr associated with object is destroyed.

The proper way to work with weak_ptr is to convert it to shared_ptr with lock method, and then to work with created shared_ptr. In that case your weak_ptr will no expire until you have that new shared_ptr. If you don't convert weak_ptr into shared_ptr, your weak_ptr may expire at any moment.

And yes, before working with newly created shared_ptr, you must check that it isn't null, because weak_ptr may had been expired before you created shared_ptr with lock method.

std::weak_ptr<Object> Storage::findObject();

...

std::weak_ptr  <Object> weak   = Storage::findObject();
std::shared_ptr<Object> shared = weak.lock();
if (shared) // check that weak was not expired when we did "shared = weak.lock()"
{
    // do something with shared, it will not expire.
}