[libcxx-commits] [libcxx] [libc++] Collapse `optional<T&>` inheritance hierarchy (PR #215286)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Aug 11 01:00:16 PDT 2026


================
@@ -54,40 +54,47 @@ _LIBCPP_PUSH_MACROS
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _Tp>
-struct __optional_ref_base {
-  using value_type                 = _Tp;
-  using __raw_type _LIBCPP_NODEBUG = remove_reference_t<_Tp>;
-  __raw_type* __value_;
+struct __optional_ref_iterator_base {};
 
-  _LIBCPP_HIDE_FROM_ABI constexpr __optional_ref_base() noexcept : __value_(nullptr) {}
+#  if _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR
+
+template <class _Tp>
+  requires(is_object_v<_Tp> && !__is_unbounded_array_v<_Tp>)
+struct __optional_ref_iterator_base<_Tp&> {
+#    ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
+  using iterator = __bounded_iter<_Tp*>;
+#    else
+  using iterator = __capacity_aware_iterator<_Tp*, optional<_Tp&>, 1>;
+#    endif
+};
+
+#  endif
+
+template <class _Tp>
+class optional<_Tp&> : public __optional_ref_iterator_base<_Tp&> {
+public:
+  using value_type = _Tp;
+  static_assert(!is_same_v<remove_cv_t<_Tp>, in_place_t>, "instantiation of optional with in_place_t is ill-formed");
+  static_assert(!is_same_v<remove_cv_t<_Tp>, nullopt_t>, "instantiation of optional with nullopt_t is ill-formed");
+
+private:
+  _Tp* __value_{nullptr};
----------------
philnik777 wrote:

```suggestion
  _Tp* __value_ = nullptr;
```
I don't think we do this anywhere else.

https://github.com/llvm/llvm-project/pull/215286


More information about the libcxx-commits mailing list