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

Jan Kokemüller via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 20 04:26:32 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;
----------------
jiixyj wrote:

At this point the full definition of `expected` is not available yet, so I'm getting errors like:

```
__expected/expected.h:960:12: error: invalid application of 'sizeof' to an incomplete type 'expected<TracedBase<false, false>,
 TracedBase<true>>'                                                                                                                                                                             
# |   960 |     return sizeof(expected) - __libcpp_datasizeof<expected>::value;                                                                                                                 
# |       |            ^~~~~~~~~~~~~~~~                                           
```

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


More information about the libcxx-commits mailing list