用于非可复制构造类型的 not_null 转换运算符
作者: matt77hias创建于 2021年7月30日更新于 2026年4月17日
标签Priority: LowType: BugStatus: In Progress
gsl::not_null<T> 具有以下转换运算符:
constexpr operator T() const
{
return get();
}由于 gsl::not_null<T> 的条件返回类型映射为 const T&,因此对于不可复制构造类型(如 std::unique_ptr)此运算符被删除:
constexpr std::conditional_t<std::is_copy_constructible<T>::value, T, const T&> get() const
{
Ensures(ptr_ != nullptr);
return ptr_;
}因此,对于不可复制构造类型,一旦构造了 gsl::not_null<T>,就无法再提取 (可能很耗时的) ptr_ 了。使用带引用限定的成员函数来实现这两个方法(即转换运算符和 get)是否有意义?
内容来源: microsoft/GSL