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

Jan Kokemüller via libcxx-commits libcxx-commits at lists.llvm.org
Sun Oct 15 04:38:23 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>()
----------------
jiixyj 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_);
> ```

done!



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


More information about the libcxx-commits mailing list