Skip to content

Commit

Permalink
Made optional_reference::ptr return nullptr of no reference is held
Browse files Browse the repository at this point in the history
Makes more sense than propagating the exception from value(). operator-> *should* throw without value, though, so that was retained.
  • Loading branch information
Vultraz committed Feb 15, 2021
1 parent e28d039 commit f1f8769
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/utils/optional_reference.hpp
Expand Up @@ -74,14 +74,19 @@ class optional_reference
return opt_.has_value();
}

/** Returns a pointer to the referenced object or nullptr if no reference is held. */
T* ptr() const
{
return &value();
if(opt_) {
return &value();
} else {
return nullptr;
}
}

T* operator->() const
{
return ptr();
return &value();
}

T& operator*() const
Expand Down

0 comments on commit f1f8769

Please sign in to comment.