not_null conversion operator for non-copy constructible types
Author: matt77hiasCreated Jul 30, 2021Updated Apr 17, 2026
LabelsPriority: LowType: BugStatus: In Progress
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_);
}Source: microsoft/GSL