[libcxx-commits] [libcxx] [libc++] Fix UB in <expected> related to "has value" flag (#68552) (PR #68733)

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Oct 14 14:50:45 PDT 2023


================
@@ -1552,6 +1528,18 @@ class expected<_Tp, _Err> {
     _LIBCPP_NO_UNIQUE_ADDRESS _ErrorType __unex_;
   };
 
+  template <class _Up, class _OtherErr>
+  _LIBCPP_HIDE_FROM_ABI constexpr __union_t<_Err> __union_from_expected(const expected<_Up, _OtherErr>& __other) {
+    return __other.__has_val_ ? __union_t<_Err>()
----------------
philnik777 wrote:

I think it would be clearer to just do
```c++
if (__other.__has_val_)
  return __union_t<_Err>{};
else
  return __union_t<_Err>(__construct_unexpected_tag{}, __other.__union_.__unex_);
```

@huixie90 Why did we make the union a template?

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


More information about the libcxx-commits mailing list