mardi 20 juillet 2021

Is it safe to return `nullptr` as a `std::shared_ptr` in C++ 11?

Taking the following code snippet as a starting example:

template <typename FOO>
std::shared_ptr<FOO> createSharedPtr(bool yesNo) {
  if (!yesNo) {
    return nullptr;
  } else {
    return make_shared<FOO>();
  }
}

I have thought the return nullptr; statement above is compliant with C++ 11 standard, as the nullptr will be used to construct a null std::shared_ptr when returning to caller, as described at cppreference shared_ptr:

constexpr shared_ptr( std::nullptr_t ) noexcept;            (2)

However, one of my colleagues keeps insisting that he should go for return shared_ptr<FOO>(); instead, as he cannot find any official documents nor examples that assigns nullptr to a std::shared_ptr.

Am I correct with my understanding? If yes, which document I should show him to prove my oppinion?

Aucun commentaire:

Enregistrer un commentaire