Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not_null conversion operator for non-copy constructible types #991

Open
matt77hias opened this issue Jul 30, 2021 · 3 comments
Open

not_null conversion operator for non-copy constructible types #991

matt77hias opened this issue Jul 30, 2021 · 3 comments

Comments

@matt77hias
Copy link
Contributor

matt77hias commented Jul 30, 2021

gsl::not_null< T > has the following conversion operator:

constexpr operator T() const
{
    return get();
}

which is deleted for non-copy constructible types such as std::unique_ptr due to gsl::not_null< T >'s conditional return type mapping to const T&:

constexpr std::conditional_t<std::is_copy_constructible<T>::value, T, const T&> get() const
{
    Ensures(ptr_ != nullptr);
    return ptr_;
}

Therefore, it is not possible to extract (the potentially expensive) ptr_ anymore for non-copy constructible types once a gsl::not_null< T > is constructed around it. Wouldn't it make sense to use ref-qualified member methods for both methods (i.e. conversion operator and get)?

// For non-copy constructible types:

constexpr operator const T&() const &
{
    return ptr_;
}

constexpr operator T() &&
{
    return std::move(ptr_);
}
@matt77hias
Copy link
Contributor Author

Note that it could make sense to use ref-qualified member methods for copy constructible types as well.

E.g.,

gsl::not_null< std::shared_ptr< Widget > > nptr = ...;
...
std::shared_ptr< Widget > ptr = std::move(nptr).get(); // Avoids copying the std::shared_ptr.

@sunnychatterjee
Copy link

Thanks for the feedback. We'll follow-up with the editors and circle back.

@JordanMaples
Copy link
Contributor

We didn't have time to discuss this during today's sync. This will be the first item on our agenda next time we meet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants