Skip to content

not_null conversion operator for non-copy constructible types #991

@matt77hias

Description

@matt77hias

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

Metadata

Metadata

Labels

Type

Projects

Status

Assigned

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions