[libcxx-commits] [libcxx] [libc++] Ensure that `std::expected` has no tail padding (PR #69673)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 20 02:08:51 PDT 2023


================
@@ -955,8 +956,17 @@ class expected {
     _LIBCPP_NO_UNIQUE_ADDRESS _ErrorType __unex_;
   };
 
+  _LIBCPP_HIDE_FROM_ABI static constexpr auto __calculate_padding() {
+    struct __calc_expected {
+      _LIBCPP_NO_UNIQUE_ADDRESS __union_t<_Tp, _Err> __union_;
+      bool __has_val_;
+    };
+    return sizeof(__calc_expected) - __libcpp_datasizeof<__calc_expected>::value;
+  }
+
   _LIBCPP_NO_UNIQUE_ADDRESS __union_t<_Tp, _Err> __union_;
   bool __has_val_;
+  char __padding_[__calculate_padding()]{};
----------------
philnik777 wrote:

Could we try to allow tail padding inside `expected` if the subobject didn't have any? e.g. it would be fine if `expected<int, int>` has tail padding, since the subobject can't overwrite any of the `expected` tail padding.

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


More information about the libcxx-commits mailing list