Prevent inefficient copying when using not_null::get (#1059)

Closes issue #550, which highlighted overhead in not_null::get for larger types such as shared_ptr. Every call to get would return a copy of the contained value.
This PR implements Herb's suggestion for changing the return type of not_null::get. The not_null's value will now only be copied if it is "trivially copyable"; otherwise, it will be returned by const reference.
Note: this change also forces the returned copy to be const.
This commit is contained in:
dmitrykobets-msft
2022-10-11 16:49:16 -07:00
committed by GitHub
parent 7d49d4b45d
commit 991fa6682e
2 changed files with 13 additions and 2 deletions
+1 -1
View File
@@ -52,7 +52,7 @@ template <typename T>
struct CustomPtr
{
CustomPtr(T* p) : p_(p) {}
operator T*() { return p_; }
operator T*() const { return p_; }
bool operator!=(std::nullptr_t) const { return p_ != nullptr; }
T* p_ = nullptr;
};