[libcxx-commits] [libcxx] [libc++] Collapse `optional<T&>` inheritance hierarchy (PR #215286)
A. Jiang via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Aug 10 20:37:05 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 _LIBCPP_DECLSPEC_EMPTY_BASES optional<_Tp&> : public __optional_ref_iterator_base<_Tp&> {
----------------
frederick-vs-ja wrote:
It seems to me that `_LIBCPP_DECLSPEC_EMPTY_BASES` doesn't really affect the size of `optional<T&>` here as we're using single inheritance now. See https://learn.microsoft.com/en-us/cpp/cpp/empty-bases?view=msvc-170.
Maybe it's be better to drop addition of `_LIBCPP_DECLSPEC_EMPTY_BASES` in this PR and add test coverage on sizes (i.e. various `static_assert(sizeof(std::optional<T&>) == sizeof(T*));`) in a following-up PR.
https://github.com/llvm/llvm-project/pull/215286
More information about the libcxx-commits
mailing list