microsoft / microsoft/GSL

not_null conversion operator for non-copy constructible types

Open
#991 9 comments 1 reaction 1 assignee View on GitHub

Nobody has claimed this yet.

Priority: Low Status: In Progress Type: Bug
Dominant language
C++
Stars
6.7k
Forks
772
Avg merge
4d 23h
Merged PRs (30d)
9

Description

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_);
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.