Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
expected_base.h1049 linesDownload Raw Back to __expected
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6// SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES.7//8//===----------------------------------------------------------------------===//9#ifndef _LIBCUDACXX___EXPECTED_EXPECTED_BASE_H10#define _LIBCUDACXX___EXPECTED_EXPECTED_BASE_H11 12#ifndef __cuda_std__13#include <__config>14#endif // __cuda_std__15 16#include "../__assert"17#include "../__concepts/__concept_macros.h"18#include "../__concepts/invocable.h"19#include "../__expected/unexpect.h"20#include "../__memory/addressof.h"21#include "../__memory/construct_at.h"22#include "../__tuple_dir/sfinae_helpers.h"23#include "../__type_traits/is_assignable.h"24#include "../__type_traits/is_constructible.h"25#include "../__type_traits/is_convertible.h"26#include "../__type_traits/is_copy_assignable.h"27#include "../__type_traits/is_copy_constructible.h"28#include "../__type_traits/is_default_constructible.h"29#include "../__type_traits/is_move_assignable.h"30#include "../__type_traits/is_move_constructible.h"31#include "../__type_traits/is_nothrow_constructible.h"32#include "../__type_traits/is_nothrow_copy_assignable.h"33#include "../__type_traits/is_nothrow_copy_constructible.h"34#include "../__type_traits/is_nothrow_default_constructible.h"35#include "../__type_traits/is_nothrow_move_assignable.h"36#include "../__type_traits/is_nothrow_move_constructible.h"37#include "../__type_traits/is_trivially_copy_assignable.h"38#include "../__type_traits/is_trivially_copy_constructible.h"39#include "../__type_traits/is_trivially_destructible.h"40#include "../__type_traits/is_trivially_move_assignable.h"41#include "../__type_traits/is_trivially_move_constructible.h"42#include "../__type_traits/is_void.h"43#include "../__utility/exception_guard.h"44#include "../__utility/forward.h"45#include "../__utility/in_place.h"46#include "../__utility/move.h"47 48#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)49#  pragma GCC system_header50#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)51#  pragma clang system_header52#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)53#  pragma system_header54#endif // no system header55 56#if _LIBCUDACXX_STD_VER > 1157 58_LIBCUDACXX_BEGIN_NAMESPACE_STD59 60// MSVC complains about [[no_unique_address]] prior to C++20 as a vendor extension61#if defined(_LIBCUDACXX_COMPILER_MSVC)62#pragma warning(push)63#pragma warning(disable : 4848)64#endif // _LIBCUDACXX_COMPILER_MSVC65 66struct __expected_construct_from_invoke_tag {67  explicit __expected_construct_from_invoke_tag() = default;68};69 70template <class _Tp, class _Err,71          bool = _LIBCUDACXX_TRAIT(is_trivially_destructible, _Tp)72              && _LIBCUDACXX_TRAIT(is_trivially_destructible, _Err)>73union __expected_union_t {74  struct __empty_t {};75 76  _LIBCUDACXX_TEMPLATE(class _Tp2 = _Tp)77    _LIBCUDACXX_REQUIRES( _LIBCUDACXX_TRAIT(is_default_constructible, _Tp2))78  _LIBCUDACXX_INLINE_VISIBILITY constexpr79  __expected_union_t() noexcept(_LIBCUDACXX_TRAIT(is_nothrow_default_constructible, _Tp2)) : __val_() {}80 81  _LIBCUDACXX_TEMPLATE(class _Tp2 = _Tp)82    _LIBCUDACXX_REQUIRES( (!_LIBCUDACXX_TRAIT(is_default_constructible, _Tp2)))83  _LIBCUDACXX_INLINE_VISIBILITY constexpr84  __expected_union_t() noexcept : __empty_() {}85 86  template<class... _Args>87  _LIBCUDACXX_INLINE_VISIBILITY constexpr88  __expected_union_t(in_place_t, _Args&&... __args)89    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, _Args...))90    : __val_(_CUDA_VSTD::forward<_Args>(__args)...) {}91 92  template<class... _Args>93  _LIBCUDACXX_INLINE_VISIBILITY constexpr94  __expected_union_t(unexpect_t, _Args&&... __args)95    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, _Args...))96    : __unex_(_CUDA_VSTD::forward<_Args>(__args)...) {}97 98  template<class _Fun, class... _Args>99  _LIBCUDACXX_INLINE_VISIBILITY constexpr100  __expected_union_t(__expected_construct_from_invoke_tag, in_place_t, _Fun&& __fun, _Args&&... __args)101    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, invoke_result_t<_Fun, _Args...>))102    : __val_(_CUDA_VSTD::invoke(_CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)) {}103 104  template<class _Fun, class... _Args>105  _LIBCUDACXX_INLINE_VISIBILITY constexpr106  __expected_union_t(__expected_construct_from_invoke_tag, unexpect_t, _Fun&& __fun, _Args&&... __args)107    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, invoke_result_t<_Fun, _Args...>))108    : __unex_(_CUDA_VSTD::invoke(_CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)) {}109 110  // the __expected_destruct's destructor handles this111  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17112  ~__expected_union_t() {}113 114  _LIBCUDACXX_NO_UNIQUE_ADDRESS __empty_t __empty_;115  _LIBCUDACXX_NO_UNIQUE_ADDRESS _Tp __val_;116  _LIBCUDACXX_NO_UNIQUE_ADDRESS _Err __unex_;117};118 119template <class _Tp, class _Err>120union __expected_union_t<_Tp, _Err, true> {121  struct __empty_t {};122 123  _LIBCUDACXX_TEMPLATE(class _Tp2 = _Tp)124    _LIBCUDACXX_REQUIRES( _LIBCUDACXX_TRAIT(is_default_constructible, _Tp2))125  _LIBCUDACXX_INLINE_VISIBILITY constexpr126  __expected_union_t() noexcept(_LIBCUDACXX_TRAIT(is_nothrow_default_constructible, _Tp2)) : __val_() {}127 128  _LIBCUDACXX_TEMPLATE(class _Tp2 = _Tp)129    _LIBCUDACXX_REQUIRES( (!_LIBCUDACXX_TRAIT(is_default_constructible, _Tp2)))130  _LIBCUDACXX_INLINE_VISIBILITY constexpr131  __expected_union_t() noexcept : __empty_() {}132 133  template<class... _Args>134  _LIBCUDACXX_INLINE_VISIBILITY constexpr135  __expected_union_t(in_place_t, _Args&&... __args)136    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, _Args...))137    : __val_(_CUDA_VSTD::forward<_Args>(__args)...) {}138 139  template<class... _Args>140  _LIBCUDACXX_INLINE_VISIBILITY constexpr141  __expected_union_t(unexpect_t, _Args&&... __args)142    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, _Args...))143    : __unex_(_CUDA_VSTD::forward<_Args>(__args)...) {}144 145  template<class _Fun, class... _Args>146  _LIBCUDACXX_INLINE_VISIBILITY constexpr147  __expected_union_t(__expected_construct_from_invoke_tag, in_place_t, _Fun&& __fun, _Args&&... __args)148    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, invoke_result_t<_Fun, _Args...>))149    : __val_(_CUDA_VSTD::invoke(_CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)) {}150 151  template<class _Fun, class... _Args>152  _LIBCUDACXX_INLINE_VISIBILITY constexpr153  __expected_union_t(__expected_construct_from_invoke_tag, unexpect_t, _Fun&& __fun, _Args&&... __args)154    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, invoke_result_t<_Fun, _Args...>))155    : __unex_(_CUDA_VSTD::invoke(_CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)) {}156 157  _LIBCUDACXX_NO_UNIQUE_ADDRESS __empty_t __empty_;158  _LIBCUDACXX_NO_UNIQUE_ADDRESS _Tp __val_;159  _LIBCUDACXX_NO_UNIQUE_ADDRESS _Err __unex_;160};161 162template <class _Tp, class _Err,163          bool = _LIBCUDACXX_TRAIT(is_trivially_destructible, _Tp),164          bool = _LIBCUDACXX_TRAIT(is_trivially_destructible, _Err)>165struct __expected_destruct;166 167template <class _Tp, class _Err>168struct __expected_destruct<_Tp, _Err, false, false> {169  _LIBCUDACXX_NO_UNIQUE_ADDRESS __expected_union_t<_Tp, _Err> __union_{};170  bool __has_val_{true};171 172  constexpr __expected_destruct() noexcept = default;173 174  _LIBCUDACXX_INLINE_VISIBILITY constexpr175  __expected_destruct(const bool __has_val) noexcept : __has_val_(__has_val) {}176 177  template<class... _Args>178  _LIBCUDACXX_INLINE_VISIBILITY constexpr179  __expected_destruct(in_place_t, _Args&&... __args)180    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, _Args...))181    : __union_(in_place, _CUDA_VSTD::forward<_Args>(__args)...)182    , __has_val_(true)183  {}184 185  template<class... _Args>186  _LIBCUDACXX_INLINE_VISIBILITY constexpr187  __expected_destruct(unexpect_t, _Args&&... __args)188    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, _Args...))189    : __union_(unexpect, _CUDA_VSTD::forward<_Args>(__args)...)190    , __has_val_(false)191  {}192 193  template<class _Fun, class... _Args>194  _LIBCUDACXX_INLINE_VISIBILITY constexpr195  __expected_destruct(__expected_construct_from_invoke_tag, in_place_t, _Fun&& __fun, _Args&&... __args)196    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, invoke_result_t<_Fun, _Args...>))197    : __union_(__expected_construct_from_invoke_tag{}, in_place, _CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)198    , __has_val_(true)199  {}200 201  template<class _Fun, class... _Args>202  _LIBCUDACXX_INLINE_VISIBILITY constexpr203  __expected_destruct(__expected_construct_from_invoke_tag, unexpect_t, _Fun&& __fun, _Args&&... __args)204    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, invoke_result_t<_Fun, _Args...>))205    : __union_(__expected_construct_from_invoke_tag{}, unexpect, _CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)206    , __has_val_(false)207  {}208 209  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17210  ~__expected_destruct() {211    if (__has_val_) {212      _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(__union_.__val_));213    } else {214      _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(__union_.__unex_));215    }216  }217};218 219template <class _Tp, class _Err>220struct __expected_destruct<_Tp, _Err, true, false> {221  _LIBCUDACXX_NO_UNIQUE_ADDRESS __expected_union_t<_Tp, _Err> __union_{};222  bool __has_val_{true};223 224  constexpr __expected_destruct() noexcept = default;225 226  _LIBCUDACXX_INLINE_VISIBILITY constexpr227  __expected_destruct(const bool __has_val) noexcept : __has_val_(__has_val) {}228 229  template<class... _Args>230  _LIBCUDACXX_INLINE_VISIBILITY constexpr231  __expected_destruct(in_place_t, _Args&&... __args)232    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, _Args...))233    : __union_(in_place, _CUDA_VSTD::forward<_Args>(__args)...)234    , __has_val_(true)235  {}236 237  template<class... _Args>238  _LIBCUDACXX_INLINE_VISIBILITY constexpr239  __expected_destruct(unexpect_t, _Args&&... __args)240    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, _Args...))241    : __union_(unexpect, _CUDA_VSTD::forward<_Args>(__args)...)242    , __has_val_(false)243  {}244 245  template<class _Fun, class... _Args>246  _LIBCUDACXX_INLINE_VISIBILITY constexpr247  __expected_destruct(__expected_construct_from_invoke_tag, in_place_t, _Fun&& __fun, _Args&&... __args)248    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, invoke_result_t<_Fun, _Args...>))249    : __union_(__expected_construct_from_invoke_tag{}, in_place, _CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)250    , __has_val_(true)251  {}252 253  template<class _Fun, class... _Args>254  _LIBCUDACXX_INLINE_VISIBILITY constexpr255  __expected_destruct(__expected_construct_from_invoke_tag, unexpect_t, _Fun&& __fun, _Args&&... __args)256    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, invoke_result_t<_Fun, _Args...>))257    : __union_(__expected_construct_from_invoke_tag{}, unexpect, _CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)258    , __has_val_(false)259  {}260 261  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17262  ~__expected_destruct() {263    if (!__has_val_) {264      _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(__union_.__unex_));265    }266  }267};268 269template <class _Tp, class _Err>270struct __expected_destruct<_Tp, _Err, false, true> {271  _LIBCUDACXX_NO_UNIQUE_ADDRESS __expected_union_t<_Tp, _Err> __union_{};272  bool __has_val_{true};273 274  constexpr __expected_destruct() noexcept = default;275 276  _LIBCUDACXX_INLINE_VISIBILITY constexpr277  __expected_destruct(const bool __has_val) noexcept : __has_val_(__has_val) {}278 279  template<class... _Args>280  _LIBCUDACXX_INLINE_VISIBILITY constexpr281  __expected_destruct(in_place_t, _Args&&... __args)282    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, _Args...))283    : __union_(in_place, _CUDA_VSTD::forward<_Args>(__args)...)284    , __has_val_(true)285  {}286 287  template<class... _Args>288  _LIBCUDACXX_INLINE_VISIBILITY constexpr289  __expected_destruct(unexpect_t, _Args&&... __args)290    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, _Args...))291    : __union_(unexpect, _CUDA_VSTD::forward<_Args>(__args)...)292    , __has_val_(false)293  {}294 295  template<class _Fun, class... _Args>296  _LIBCUDACXX_INLINE_VISIBILITY constexpr297  __expected_destruct(__expected_construct_from_invoke_tag, in_place_t, _Fun&& __fun, _Args&&... __args)298    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, invoke_result_t<_Fun, _Args...>))299    : __union_(__expected_construct_from_invoke_tag{}, in_place, _CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)300    , __has_val_(true)301  {}302 303  template<class _Fun, class... _Args>304  _LIBCUDACXX_INLINE_VISIBILITY constexpr305  __expected_destruct(__expected_construct_from_invoke_tag, unexpect_t, _Fun&& __fun, _Args&&... __args)306    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, invoke_result_t<_Fun, _Args...>))307    : __union_(__expected_construct_from_invoke_tag{}, unexpect, _CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)308    , __has_val_(false)309  {}310 311  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17312  ~__expected_destruct() {313    if (__has_val_) {314      _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(__union_.__val_));315    }316  }317};318 319template <class _Tp, class _Err>320struct __expected_destruct<_Tp, _Err, true, true> {321  // This leads to an ICE with nvcc, see nvbug4103076322  /* _LIBCUDACXX_NO_UNIQUE_ADDRESS */ __expected_union_t<_Tp, _Err> __union_{};323  bool __has_val_{true};324 325  constexpr __expected_destruct() noexcept = default;326 327  _LIBCUDACXX_INLINE_VISIBILITY constexpr328  __expected_destruct(const bool __has_val) noexcept : __has_val_(__has_val) {}329 330  template<class... _Args>331  _LIBCUDACXX_INLINE_VISIBILITY constexpr332  __expected_destruct(in_place_t, _Args&&... __args)333    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, _Args...))334    : __union_(in_place, _CUDA_VSTD::forward<_Args>(__args)...)335    , __has_val_(true)336  {}337 338  template<class... _Args>339  _LIBCUDACXX_INLINE_VISIBILITY constexpr340  __expected_destruct(unexpect_t, _Args&&... __args)341    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, _Args...))342    : __union_(unexpect, _CUDA_VSTD::forward<_Args>(__args)...)343    , __has_val_(false)344  {}345 346  template<class _Fun, class... _Args>347  _LIBCUDACXX_INLINE_VISIBILITY constexpr348  __expected_destruct(__expected_construct_from_invoke_tag, in_place_t, _Fun&& __fun, _Args&&... __args)349    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Tp, invoke_result_t<_Fun, _Args...>))350    : __union_(__expected_construct_from_invoke_tag{}, in_place, _CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)351    , __has_val_(true)352  {}353 354  template<class _Fun, class... _Args>355  _LIBCUDACXX_INLINE_VISIBILITY constexpr356  __expected_destruct(__expected_construct_from_invoke_tag, unexpect_t, _Fun&& __fun, _Args&&... __args)357    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, invoke_result_t<_Fun, _Args...>))358    : __union_(__expected_construct_from_invoke_tag{}, unexpect, _CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)359    , __has_val_(false)360  {}361};362 363#if defined(_LIBCUDACXX_COMPILER_MSVC)364#pragma warning(pop)365#endif // _LIBCUDACXX_COMPILER_MSVC366 367template <class _Tp, class _Err>368struct __expected_storage : __expected_destruct<_Tp, _Err>369{370  using __base = __expected_destruct<_Tp, _Err>;371 372// nvbug3961621373#if defined(_LIBCUDACXX_COMPILER_NVRTC)  \374 || (defined(_LIBCUDACXX_CUDACC_BELOW_11_3) && defined(_LIBCUDACXX_COMPILER_CLANG))375  constexpr __expected_storage() noexcept = default;376 377  template<class... _Args, __enable_if_t<_LIBCUDACXX_TRAIT(is_constructible, __base, _Args...), int> = 0>378   _LIBCUDACXX_INLINE_VISIBILITY constexpr379  __expected_storage(_Args&&... __args) noexcept(noexcept(__base(_CUDA_VSTD::declval<_Args>()...)))380    : __base(_CUDA_VSTD::forward<_Args>(__args)...)381  {}382#else // ^^^ _LIBCUDACXX_COMPILER_NVRTC || nvcc < 11.3 ^^^ / vvv !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3 vvv383  using __base::__base;384#endif // !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3385 386  _LIBCUDACXX_TEMPLATE(class _T1, class _T2, class... _Args)387    _LIBCUDACXX_REQUIRES( _LIBCUDACXX_TRAIT(is_nothrow_constructible, _T1, _Args...))388  static _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17389  void __reinit_expected(_T1& __newval, _T2& __oldval, _Args&&... __args) noexcept {390    _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(__oldval));391    _LIBCUDACXX_CONSTRUCT_AT(__newval, _CUDA_VSTD::forward<_Args>(__args)...);392  }393 394  _LIBCUDACXX_TEMPLATE(class _T1, class _T2, class... _Args)395    _LIBCUDACXX_REQUIRES( (!_LIBCUDACXX_TRAIT(is_nothrow_constructible, _T1, _Args...)) _LIBCUDACXX_AND396                _LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _T1)397    )398  static _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17399  void __reinit_expected(_T1& __newval, _T2& __oldval, _Args&&... __args) {400    _T1 __tmp(_CUDA_VSTD::forward<_Args>(__args)...);401    _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(__oldval));402    _LIBCUDACXX_CONSTRUCT_AT(__newval, _CUDA_VSTD::move(__tmp));403  }404 405  _LIBCUDACXX_TEMPLATE(class _T1, class _T2, class... _Args)406    _LIBCUDACXX_REQUIRES( (!_LIBCUDACXX_TRAIT(is_nothrow_constructible, _T1, _Args...)) _LIBCUDACXX_AND407              (!_LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _T1))408    )409  static _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17410  void __reinit_expected(_T1& __newval, _T2& __oldval, _Args&&... __args) {411    static_assert(_LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _T2),412        "To provide strong exception guarantee, T2 has to satisfy `is_nothrow_move_constructible_v` so that it can "413        "be reverted to the previous state in case an exception is thrown during the assignment.");414    _T2 __tmp(_CUDA_VSTD::move(__oldval));415    _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(__oldval));416    auto __trans =417        _CUDA_VSTD::__make_exception_guard([&] { _LIBCUDACXX_CONSTRUCT_AT(__oldval, _CUDA_VSTD::move(__tmp)); });418    _LIBCUDACXX_CONSTRUCT_AT(__newval, _CUDA_VSTD::forward<_Args>(__args)...);419    __trans.__complete();420  }421 422  _LIBCUDACXX_TEMPLATE(class _Err2 = _Err)423    _LIBCUDACXX_REQUIRES( _LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Err2))424  static _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17425  void __swap_val_unex_impl(__expected_storage<_Tp, _Err2>& __with_val, __expected_storage& __with_err) {426    _Err __tmp(_CUDA_VSTD::move(__with_err.__union_.__unex_));427    _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(__with_err.__union_.__unex_));428    auto __trans = _CUDA_VSTD::__make_exception_guard([&] {429      _LIBCUDACXX_CONSTRUCT_AT(__with_err.__union_.__unex_, _CUDA_VSTD::move(__tmp));430    });431    _LIBCUDACXX_CONSTRUCT_AT(__with_err.__union_.__val_, _CUDA_VSTD::move(__with_val.__union_.__val_));432    __trans.__complete();433    _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(__with_val.__union_.__val_));434    _LIBCUDACXX_CONSTRUCT_AT(__with_val.__union_.__unex_, _CUDA_VSTD::move(__tmp));435    __with_val.__has_val_ = false;436    __with_err.__has_val_ = true;437  }438 439  _LIBCUDACXX_TEMPLATE(class _Err2 = _Err)440    _LIBCUDACXX_REQUIRES( (!_LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Err2)))441  static _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17442  void __swap_val_unex_impl(__expected_storage<_Tp, _Err2>& __with_val, __expected_storage& __with_err) {443    static_assert(_LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Tp),444                  "To provide strong exception guarantee, Tp has to satisfy `is_nothrow_move_constructible_v` so "445                  "that it can be reverted to the previous state in case an exception is thrown during swap.");446    _Tp __tmp(_CUDA_VSTD::move(__with_val.__union_.__val_));447    _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(__with_val.__union_.__val_));448    auto __trans = _CUDA_VSTD::__make_exception_guard([&] {449      _LIBCUDACXX_CONSTRUCT_AT(__with_val.__union_.__val_, _CUDA_VSTD::move(__tmp));450    });451    _LIBCUDACXX_CONSTRUCT_AT(__with_val.__union_.__unex_, _CUDA_VSTD::move(__with_err.__union_.__unex_));452    __trans.__complete();453    _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(__with_err.__union_.__unex_));454    _LIBCUDACXX_CONSTRUCT_AT(__with_err.__union_.__val_, _CUDA_VSTD::move(__tmp));455    __with_val.__has_val_ = false;456    __with_err.__has_val_ = true;457  }458};459 460template <class _Tp, class _Err, bool =461          (_LIBCUDACXX_TRAIT(is_trivially_copy_constructible, _Tp) || _LIBCUDACXX_TRAIT(is_same, _Tp, void)) &&462           _LIBCUDACXX_TRAIT(is_trivially_copy_constructible, _Err)>463struct __expected_copy : __expected_storage<_Tp, _Err>464{465  using __base = __expected_storage<_Tp, _Err>;466// nvbug3961621467#if defined(_LIBCUDACXX_COMPILER_NVRTC)  \468 || (defined(_LIBCUDACXX_CUDACC_BELOW_11_3) && defined(_LIBCUDACXX_COMPILER_CLANG))469  constexpr __expected_copy() noexcept = default;470 471  template<class... _Args, __enable_if_t<_LIBCUDACXX_TRAIT(is_constructible, __base, _Args...), int> = 0>472  _LIBCUDACXX_INLINE_VISIBILITY constexpr473  __expected_copy(_Args&&... __args) noexcept(noexcept(__base(_CUDA_VSTD::declval<_Args>()...)))474    : __base(_CUDA_VSTD::forward<_Args>(__args)...)475  {}476#else // ^^^ _LIBCUDACXX_COMPILER_NVRTC || nvcc < 11.3 ^^^ / vvv !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3 vvv477  using __base::__base;478#endif // !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3479};480 481template <class _Tp, class _Err>482struct __expected_copy<_Tp, _Err, false> : __expected_storage<_Tp, _Err>483{484  using __base = __expected_storage<_Tp, _Err>;485 486// nvbug3961621487#if defined(_LIBCUDACXX_COMPILER_NVRTC)  \488 || (defined(_LIBCUDACXX_CUDACC_BELOW_11_3) && defined(_LIBCUDACXX_COMPILER_CLANG))489  template<class... _Args, __enable_if_t<_LIBCUDACXX_TRAIT(is_constructible, __base, _Args...), int> = 0>490   _LIBCUDACXX_INLINE_VISIBILITY constexpr491  __expected_copy(_Args&&... __args) noexcept(noexcept(__base(_CUDA_VSTD::declval<_Args>()...)))492    : __base(_CUDA_VSTD::forward<_Args>(__args)...)493  {}494#else // ^^^ _LIBCUDACXX_COMPILER_NVRTC || nvcc < 11.3 ^^^ / vvv !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3 vvv495  using __base::__base;496#endif // !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3497 498  constexpr __expected_copy() noexcept = default;499 500  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17501  __expected_copy(const __expected_copy& __other)502    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_copy_constructible, _Tp)503          && _LIBCUDACXX_TRAIT(is_nothrow_copy_constructible, _Err))504    : __base(__other.__has_val_)505  {506    if (__other.__has_val_) {507      _LIBCUDACXX_CONSTRUCT_AT(this->__union_.__val_, __other.__union_.__val_);508    } else {509      _LIBCUDACXX_CONSTRUCT_AT(this->__union_.__unex_, __other.__union_.__unex_);510    }511  }512 513  __expected_copy(__expected_copy&&) = default;514  __expected_copy& operator=(const __expected_copy&) = default;515  __expected_copy& operator=(__expected_copy&&) = default;516};517 518template <class _Tp, class _Err, bool =519          (_LIBCUDACXX_TRAIT(is_trivially_move_constructible, _Tp) || _LIBCUDACXX_TRAIT(is_same, _Tp, void)) &&520           _LIBCUDACXX_TRAIT(is_trivially_move_constructible, _Err)>521struct __expected_move : __expected_copy<_Tp, _Err>522{523  using __base = __expected_copy<_Tp, _Err>;524// nvbug3961621525#if defined(_LIBCUDACXX_COMPILER_NVRTC)  \526 || (defined(_LIBCUDACXX_CUDACC_BELOW_11_3) && defined(_LIBCUDACXX_COMPILER_CLANG))527  constexpr __expected_move() noexcept = default;528 529  template<class... _Args, __enable_if_t<_LIBCUDACXX_TRAIT(is_constructible, __base, _Args...), int> = 0>530   _LIBCUDACXX_INLINE_VISIBILITY constexpr531  __expected_move(_Args&&... __args) noexcept(noexcept(__base(_CUDA_VSTD::declval<_Args>()...)))532    : __base(_CUDA_VSTD::forward<_Args>(__args)...)533  {}534#else // ^^^ _LIBCUDACXX_COMPILER_NVRTC || nvcc < 11.3 ^^^ / vvv !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3 vvv535  using __base::__base;536#endif // !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3537};538 539template <class _Tp, class _Err>540struct __expected_move<_Tp, _Err, false> : __expected_copy<_Tp, _Err>541{542  using __base = __expected_copy<_Tp, _Err>;543// nvbug3961621544#if defined(_LIBCUDACXX_COMPILER_NVRTC)  \545 || (defined(_LIBCUDACXX_CUDACC_BELOW_11_3) && defined(_LIBCUDACXX_COMPILER_CLANG))546  template<class... _Args, __enable_if_t<_LIBCUDACXX_TRAIT(is_constructible, __base, _Args...), int> = 0>547   _LIBCUDACXX_INLINE_VISIBILITY constexpr548  __expected_move(_Args&&... __args) noexcept(noexcept(__base(_CUDA_VSTD::declval<_Args>()...)))549    : __base(_CUDA_VSTD::forward<_Args>(__args)...)550  {}551#else // ^^^ _LIBCUDACXX_COMPILER_NVRTC || nvcc < 11.3 ^^^ / vvv !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3 vvv552  using __base::__base;553#endif // !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3554 555  __expected_move() = default;556  __expected_move(const __expected_move&) = default;557 558  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17559  __expected_move(__expected_move&& __other)560    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Tp)561          && _LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Err))562    : __base(__other.__has_val_)563  {564    if (__other.__has_val_) {565      _LIBCUDACXX_CONSTRUCT_AT(this->__union_.__val_, _CUDA_VSTD::move(__other.__union_.__val_));566    } else {567      _LIBCUDACXX_CONSTRUCT_AT(this->__union_.__unex_, _CUDA_VSTD::move(__other.__union_.__unex_));568    }569  }570 571  __expected_move& operator=(const __expected_move&) = default;572  __expected_move& operator=(__expected_move&&) = default;573};574 575template <class _Tp, class _Err, bool =576          (_LIBCUDACXX_TRAIT(is_trivially_destructible, _Tp)  || _LIBCUDACXX_TRAIT(is_same, _Tp, void)) &&577           _LIBCUDACXX_TRAIT(is_trivially_destructible, _Err) &&578          (_LIBCUDACXX_TRAIT(is_trivially_copy_constructible, _Tp)  || _LIBCUDACXX_TRAIT(is_same, _Tp, void))&&579           _LIBCUDACXX_TRAIT(is_trivially_copy_constructible, _Err) &&580          (_LIBCUDACXX_TRAIT(is_trivially_copy_assignable, _Tp)  || _LIBCUDACXX_TRAIT(is_same, _Tp, void))&&581           _LIBCUDACXX_TRAIT(is_trivially_copy_assignable, _Err)>582struct __expected_copy_assign : __expected_move<_Tp, _Err>583{584  using __base = __expected_move<_Tp, _Err>;585// nvbug3961621586#if defined(_LIBCUDACXX_COMPILER_NVRTC)  \587 || (defined(_LIBCUDACXX_CUDACC_BELOW_11_3) && defined(_LIBCUDACXX_COMPILER_CLANG))588  constexpr __expected_copy_assign() noexcept = default;589 590  template<class... _Args, __enable_if_t<_LIBCUDACXX_TRAIT(is_constructible, __base, _Args...), int> = 0>591   _LIBCUDACXX_INLINE_VISIBILITY constexpr592  __expected_copy_assign(_Args&&... __args) noexcept(noexcept(__base(_CUDA_VSTD::declval<_Args>()...)))593    : __base(_CUDA_VSTD::forward<_Args>(__args)...)594  {}595#else // ^^^ _LIBCUDACXX_COMPILER_NVRTC || nvcc < 11.3 ^^^ / vvv !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3 vvv596  using __base::__base;597#endif // !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3598};599 600template <class _Tp, class _Err>601struct __expected_copy_assign<_Tp, _Err, false> : __expected_move<_Tp, _Err>602{603  using __base = __expected_move<_Tp, _Err>;604// nvbug3961621605#if defined(_LIBCUDACXX_COMPILER_NVRTC)  \606 || (defined(_LIBCUDACXX_CUDACC_BELOW_11_3) && defined(_LIBCUDACXX_COMPILER_CLANG))607  template<class... _Args, __enable_if_t<_LIBCUDACXX_TRAIT(is_constructible, __base, _Args...), int> = 0>608   _LIBCUDACXX_INLINE_VISIBILITY constexpr609  __expected_copy_assign(_Args&&... __args) noexcept(noexcept(__base(_CUDA_VSTD::declval<_Args>()...)))610    : __base(_CUDA_VSTD::forward<_Args>(__args)...)611  {}612#else // ^^^ _LIBCUDACXX_COMPILER_NVRTC || nvcc < 11.3 ^^^ / vvv !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3 vvv613  using __base::__base;614#endif // !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3615 616  __expected_copy_assign() = default;617  __expected_copy_assign(const __expected_copy_assign&) = default;618  __expected_copy_assign(__expected_copy_assign&&) = default;619 620  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17621  __expected_copy_assign& operator=(const __expected_copy_assign& __other)622    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_copy_assignable, _Tp) &&623             _LIBCUDACXX_TRAIT(is_nothrow_copy_constructible, _Tp) &&624             _LIBCUDACXX_TRAIT(is_nothrow_copy_assignable, _Err) &&625             _LIBCUDACXX_TRAIT(is_nothrow_copy_constructible, _Err)) // strengthened626  {627    if (this->__has_val_ && __other.__has_val_) {628      this->__union_.__val_ = __other.__union_.__val_;629    } else if (this->__has_val_ && !__other.__has_val_) {630      this->__reinit_expected(this->__union_.__unex_, this->__union_.__val_, __other.__union_.__unex_);631      this->__has_val_ = false;632    } else if (!this->__has_val_ && __other.__has_val_) {633      this->__reinit_expected(this->__union_.__val_, this->__union_.__unex_, __other.__union_.__val_);634      this->__has_val_ = true;635    } else { // !this->__has_val_ && !__other.__has_val_636      this->__union_.__unex_ = __other.__union_.__unex_;637    }638    return *this;639  }640 641  __expected_copy_assign& operator=(__expected_copy_assign&&) = default;642};643 644template <class _Tp, class _Err, bool =645          (_LIBCUDACXX_TRAIT(is_trivially_destructible, _Tp) || _LIBCUDACXX_TRAIT(is_same, _Tp, void)) &&646          _LIBCUDACXX_TRAIT(is_trivially_destructible, _Err) &&647          (_LIBCUDACXX_TRAIT(is_trivially_move_constructible, _Tp) || _LIBCUDACXX_TRAIT(is_same, _Tp, void)) &&648          _LIBCUDACXX_TRAIT(is_trivially_move_constructible, _Err) &&649          (_LIBCUDACXX_TRAIT(is_trivially_move_assignable, _Tp) || _LIBCUDACXX_TRAIT(is_same, _Tp, void)) &&650          _LIBCUDACXX_TRAIT(is_trivially_move_assignable, _Err)>651struct __expected_move_assign : __expected_copy_assign<_Tp, _Err>652{653  using __base = __expected_copy_assign<_Tp, _Err>;654// nvbug3961621655#if defined(_LIBCUDACXX_COMPILER_NVRTC)  \656 || (defined(_LIBCUDACXX_CUDACC_BELOW_11_3) && defined(_LIBCUDACXX_COMPILER_CLANG))657  constexpr __expected_move_assign() noexcept = default;658 659  template<class... _Args, __enable_if_t<_LIBCUDACXX_TRAIT(is_constructible, __base, _Args...), int> = 0>660   _LIBCUDACXX_INLINE_VISIBILITY constexpr661  __expected_move_assign(_Args&&... __args) noexcept(noexcept(__base(_CUDA_VSTD::declval<_Args>()...)))662    : __base(_CUDA_VSTD::forward<_Args>(__args)...)663  {}664#else // ^^^ _LIBCUDACXX_COMPILER_NVRTC || nvcc < 11.3 ^^^ / vvv !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3 vvv665  using __base::__base;666#endif // !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3667};668 669template <class _Tp, class _Err>670struct __expected_move_assign<_Tp, _Err, false> : __expected_copy_assign<_Tp, _Err>671{672  using __base = __expected_copy_assign<_Tp, _Err>;673// nvbug3961621674#if defined(_LIBCUDACXX_COMPILER_NVRTC)  \675 || (defined(_LIBCUDACXX_CUDACC_BELOW_11_3) && defined(_LIBCUDACXX_COMPILER_CLANG))676  template<class... _Args, __enable_if_t<_LIBCUDACXX_TRAIT(is_constructible, __base, _Args...), int> = 0>677   _LIBCUDACXX_INLINE_VISIBILITY constexpr678  __expected_move_assign(_Args&&... __args) noexcept(noexcept(__base(_CUDA_VSTD::declval<_Args>()...)))679    : __base(_CUDA_VSTD::forward<_Args>(__args)...)680  {}681#else // ^^^ _LIBCUDACXX_COMPILER_NVRTC || nvcc < 11.3 ^^^ / vvv !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3 vvv682  using __base::__base;683#endif // !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3684 685  __expected_move_assign() = default;686  __expected_move_assign(const __expected_move_assign&) = default;687  __expected_move_assign(__expected_move_assign&&) = default;688  __expected_move_assign& operator=(const __expected_move_assign&) = default;689 690  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17691  __expected_move_assign& operator=(__expected_move_assign&& __other)692    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_move_assignable, _Tp) &&693             _LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Tp) &&694             _LIBCUDACXX_TRAIT(is_nothrow_move_assignable, _Err) &&695             _LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Err)) // strengthened696  {697    if (this->__has_val_ && __other.__has_val_) {698      this->__union_.__val_ = _CUDA_VSTD::move(__other.__union_.__val_);699    } else if (this->__has_val_ && !__other.__has_val_) {700      this->__reinit_expected(this->__union_.__unex_, this->__union_.__val_, _CUDA_VSTD::move(__other.__union_.__unex_));701      this->__has_val_ = false;702    } else if (!this->__has_val_ && __other.__has_val_) {703      this->__reinit_expected(this->__union_.__val_, this->__union_.__unex_, _CUDA_VSTD::move(__other.__union_.__val_));704      this->__has_val_ = true;705    } else { // !this->__has_val_ && !__other.__has_val_706      this->__union_.__unex_ = _CUDA_VSTD::move(__other.__union_.__unex_);707    }708    return *this;709  }710};711 712template <class _Tp, class _Err>713using __expected_sfinae_ctor_base_t = __sfinae_ctor_base<714  _LIBCUDACXX_TRAIT(is_copy_constructible, _Tp) && _LIBCUDACXX_TRAIT(is_copy_constructible, _Err),715  _LIBCUDACXX_TRAIT(is_move_constructible, _Tp) && _LIBCUDACXX_TRAIT(is_move_constructible, _Err)716>;717 718template <class _Tp, class _Err>719using __expected_sfinae_assign_base_t = __sfinae_assign_base<720  _LIBCUDACXX_TRAIT(is_copy_constructible, _Tp) && _LIBCUDACXX_TRAIT(is_copy_constructible, _Err) &&721  _LIBCUDACXX_TRAIT(is_copy_assignable, _Tp)    && _LIBCUDACXX_TRAIT(is_copy_assignable, _Err) &&722  (_LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Tp) || _LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Err)),723  _LIBCUDACXX_TRAIT(is_move_constructible, _Tp) && _LIBCUDACXX_TRAIT(is_move_constructible, _Err) &&724  _LIBCUDACXX_TRAIT(is_move_assignable, _Tp)    && _LIBCUDACXX_TRAIT(is_move_assignable, _Err) &&725 (_LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Tp) || _LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Err))726>;727 728// expected<void, E> base classtemplate <class _Tp, class _Err>729// MSVC complains about [[no_unique_address]] prior to C++20 as a vendor extension730#if defined(_LIBCUDACXX_COMPILER_MSVC)731#pragma warning(push)732#pragma warning(disable : 4848)733#endif // _LIBCUDACXX_COMPILER_MSVC734 735template <class _Err>736struct __expected_destruct<void, _Err, false, false> {737  _LIBCUDACXX_NO_UNIQUE_ADDRESS union __expected_union_t {738    struct __empty_t {};739 740    _LIBCUDACXX_INLINE_VISIBILITY constexpr741    __expected_union_t() noexcept : __empty_() {}742 743    template<class... _Args>744    _LIBCUDACXX_INLINE_VISIBILITY constexpr745    __expected_union_t(unexpect_t, _Args&&... __args)746      noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, _Args...))747      : __unex_(_CUDA_VSTD::forward<_Args>(__args)...) {}748 749    template<class _Fun, class... _Args>750    _LIBCUDACXX_INLINE_VISIBILITY constexpr751    __expected_union_t(__expected_construct_from_invoke_tag, unexpect_t, _Fun&& __fun, _Args&&... __args)752      noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, invoke_result_t<_Fun, _Args...>))753      : __unex_(_CUDA_VSTD::invoke(_CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)) {}754 755    // the __expected_destruct's destructor handles this756    _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17757    ~__expected_union_t() {}758 759    _LIBCUDACXX_NO_UNIQUE_ADDRESS __empty_t __empty_;760    _LIBCUDACXX_NO_UNIQUE_ADDRESS _Err __unex_;761  } __union_{};762  bool __has_val_{true};763 764  constexpr __expected_destruct() noexcept = default;765 766  _LIBCUDACXX_INLINE_VISIBILITY constexpr767  __expected_destruct(const bool __has_val) noexcept : __has_val_(__has_val) {}768 769  template<class... _Args>770  _LIBCUDACXX_INLINE_VISIBILITY constexpr771  __expected_destruct(unexpect_t, _Args&&... __args)772    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, _Args...))773    : __union_(unexpect, _CUDA_VSTD::forward<_Args>(__args)...)774    , __has_val_(false)775  {}776 777  template<class _Fun, class... _Args>778  _LIBCUDACXX_INLINE_VISIBILITY constexpr779  __expected_destruct(__expected_construct_from_invoke_tag, unexpect_t, _Fun&& __fun, _Args&&... __args)780    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, invoke_result_t<_Fun, _Args...>))781    : __union_(__expected_construct_from_invoke_tag{}, unexpect, _CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)782    , __has_val_(false)783  {}784 785  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17786  ~__expected_destruct() {787    if (!__has_val_) {788      _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(__union_.__unex_));789    }790  }791};792 793template <class _Err>794struct __expected_destruct<void, _Err, false, true> {795  // Using `_LIBCUDACXX_NO_UNIQUE_ADDRESS` here crashes nvcc796  /* _LIBCUDACXX_NO_UNIQUE_ADDRESS */ union __expected_union_t {797    struct __empty_t {};798 799    _LIBCUDACXX_INLINE_VISIBILITY constexpr800    __expected_union_t() noexcept : __empty_() {}801 802    template<class... _Args>803    _LIBCUDACXX_INLINE_VISIBILITY constexpr804    __expected_union_t(unexpect_t, _Args&&... __args)805      noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, _Args...))806      : __unex_(_CUDA_VSTD::forward<_Args>(__args)...) {}807 808    template<class _Fun, class... _Args>809    _LIBCUDACXX_INLINE_VISIBILITY constexpr810    __expected_union_t(__expected_construct_from_invoke_tag, unexpect_t, _Fun&& __fun, _Args&&... __args)811      noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, invoke_result_t<_Fun, _Args...>))812      : __unex_(_CUDA_VSTD::invoke(_CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)) {}813 814    _LIBCUDACXX_NO_UNIQUE_ADDRESS __empty_t __empty_;815    _LIBCUDACXX_NO_UNIQUE_ADDRESS _Err __unex_;816  } __union_{};817  bool __has_val_{true};818 819  constexpr __expected_destruct() noexcept = default;820 821  template<class... _Args>822  _LIBCUDACXX_INLINE_VISIBILITY constexpr823  __expected_destruct(in_place_t)824    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, _Args...))825    : __union_()826    , __has_val_(true)827  {}828 829  template<class... _Args>830  _LIBCUDACXX_INLINE_VISIBILITY constexpr831  __expected_destruct(unexpect_t, _Args&&... __args)832    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, _Args...))833    : __union_(unexpect, _CUDA_VSTD::forward<_Args>(__args)...)834    , __has_val_(false)835  {}836 837  template<class _Fun, class... _Args>838  _LIBCUDACXX_INLINE_VISIBILITY constexpr839  __expected_destruct(__expected_construct_from_invoke_tag, unexpect_t, _Fun&& __fun, _Args&&... __args)840    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_constructible, _Err, invoke_result_t<_Fun, _Args...>))841    : __union_(__expected_construct_from_invoke_tag{}, unexpect, _CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...)842    , __has_val_(false)843  {}844 845  _LIBCUDACXX_INLINE_VISIBILITY constexpr846  __expected_destruct(const bool __has_val) noexcept : __has_val_(__has_val) {}847};848 849#if defined(_LIBCUDACXX_COMPILER_MSVC)850#pragma warning(pop)851#endif // _LIBCUDACXX_COMPILER_MSVC852 853template <class _Err>854struct __expected_storage<void, _Err> : __expected_destruct<void, _Err>855{856  using __base = __expected_destruct<void, _Err>;857// nvbug3961621858#if defined(_LIBCUDACXX_COMPILER_NVRTC)  \859 || (defined(_LIBCUDACXX_CUDACC_BELOW_11_3) && defined(_LIBCUDACXX_COMPILER_CLANG))860  constexpr __expected_storage() noexcept = default;861 862  template<class... _Args, __enable_if_t<_LIBCUDACXX_TRAIT(is_constructible, __base, _Args...), int> = 0>863   _LIBCUDACXX_INLINE_VISIBILITY constexpr864  __expected_storage(_Args&&... __args) noexcept(noexcept(__base(_CUDA_VSTD::declval<_Args>()...)))865    : __base(_CUDA_VSTD::forward<_Args>(__args)...)866  {}867#else // ^^^ _LIBCUDACXX_COMPILER_NVRTC || nvcc < 11.3 ^^^ / vvv !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3 vvv868  using __base::__base;869#endif // !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3870 871  static _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17872  void __swap_val_unex_impl(__expected_storage& __with_val, __expected_storage& __with_err)873    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Err)) {874    _LIBCUDACXX_CONSTRUCT_AT(__with_val.__union_.__unex_, _CUDA_VSTD::move(__with_err.__union_.__unex_));875    _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(__with_err.__union_.__unex_));876    __with_val.__has_val_ = false;877    __with_err.__has_val_ = true;878  }879};880 881template <class _Err>882struct __expected_copy<void, _Err, false> : __expected_storage<void, _Err>883{884  using __base = __expected_storage<void, _Err>;885// nvbug3961621886#if defined(_LIBCUDACXX_COMPILER_NVRTC)  \887 || (defined(_LIBCUDACXX_CUDACC_BELOW_11_3) && defined(_LIBCUDACXX_COMPILER_CLANG))888  template<class... _Args, __enable_if_t<_LIBCUDACXX_TRAIT(is_constructible, __base, _Args...), int> = 0>889   _LIBCUDACXX_INLINE_VISIBILITY constexpr890  __expected_copy(_Args&&... __args) noexcept(noexcept(__base(_CUDA_VSTD::declval<_Args>()...)))891    : __base(_CUDA_VSTD::forward<_Args>(__args)...)892  {}893#else // ^^^ _LIBCUDACXX_COMPILER_NVRTC || nvcc < 11.3 ^^^ / vvv !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3 vvv894  using __base::__base;895#endif // !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3896 897  constexpr __expected_copy() = default;898 899  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17900  __expected_copy(const __expected_copy& __other)901    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_copy_constructible, _Err))902    : __base(__other.__has_val_)903  {904    if (!__other.__has_val_) {905      _LIBCUDACXX_CONSTRUCT_AT(this->__union_.__unex_, __other.__union_.__unex_);906    }907  }908 909  __expected_copy(__expected_copy&&) = default;910  __expected_copy& operator=(const __expected_copy&) = default;911  __expected_copy& operator=(__expected_copy&&) = default;912};913 914template <class _Err>915struct __expected_move<void, _Err, false> : __expected_copy<void, _Err>916{917  using __base = __expected_copy<void, _Err>;918// nvbug3961621919#if defined(_LIBCUDACXX_COMPILER_NVRTC)  \920 || (defined(_LIBCUDACXX_CUDACC_BELOW_11_3) && defined(_LIBCUDACXX_COMPILER_CLANG))921  template<class... _Args, __enable_if_t<_LIBCUDACXX_TRAIT(is_constructible, __base, _Args...), int> = 0>922   _LIBCUDACXX_INLINE_VISIBILITY constexpr923  __expected_move(_Args&&... __args) noexcept(noexcept(__base(_CUDA_VSTD::declval<_Args>()...)))924    : __base(_CUDA_VSTD::forward<_Args>(__args)...)925  {}926#else // ^^^ _LIBCUDACXX_COMPILER_NVRTC || nvcc < 11.3 ^^^ / vvv !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3 vvv927  using __base::__base;928#endif // !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3929 930  __expected_move() = default;931  __expected_move(const __expected_move&) = default;932 933  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17934  __expected_move(__expected_move&& __other)935    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Err))936    : __base(__other.__has_val_)937  {938    if (!__other.__has_val_) {939      _LIBCUDACXX_CONSTRUCT_AT(this->__union_.__unex_, _CUDA_VSTD::move(__other.__union_.__unex_));940    }941  }942 943  __expected_move& operator=(const __expected_move&) = default;944  __expected_move& operator=(__expected_move&&) = default;945};946 947template <class _Err>948struct __expected_copy_assign<void, _Err, false> : __expected_move<void, _Err>949{950  using __base = __expected_move<void, _Err>;951// nvbug3961621952#if defined(_LIBCUDACXX_COMPILER_NVRTC)  \953 || (defined(_LIBCUDACXX_CUDACC_BELOW_11_3) && defined(_LIBCUDACXX_COMPILER_CLANG))954  template<class... _Args, __enable_if_t<_LIBCUDACXX_TRAIT(is_constructible, __base, _Args...), int> = 0>955   _LIBCUDACXX_INLINE_VISIBILITY constexpr956  __expected_copy_assign(_Args&&... __args) noexcept(noexcept(__base(_CUDA_VSTD::declval<_Args>()...)))957    : __base(_CUDA_VSTD::forward<_Args>(__args)...)958  {}959#else // ^^^ _LIBCUDACXX_COMPILER_NVRTC || nvcc < 11.3 ^^^ / vvv !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3 vvv960  using __base::__base;961#endif // !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3962 963  __expected_copy_assign() = default;964  __expected_copy_assign(const __expected_copy_assign&) = default;965  __expected_copy_assign(__expected_copy_assign&&) = default;966 967  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17968  __expected_copy_assign& operator=(const __expected_copy_assign& __other)969    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_copy_assignable, _Err) &&970             _LIBCUDACXX_TRAIT(is_nothrow_copy_constructible, _Err)) // strengthened971  {972    if (this->__has_val_ && __other.__has_val_) {973      // nothing to do974    } else if (this->__has_val_ && !__other.__has_val_) {975      _LIBCUDACXX_CONSTRUCT_AT(this->__union_.__unex_, __other.__union_.__unex_);976      this->__has_val_ = false;977    } else if (!this->__has_val_ && __other.__has_val_) {978      _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(this->__union_.__unex_));979      this->__has_val_ = true;980    } else { // !this->__has_val_ && !__other.__has_val_981      this->__union_.__unex_ = __other.__union_.__unex_;982    }983    return *this;984  }985 986  __expected_copy_assign& operator=(__expected_copy_assign&&) = default;987};988 989template <class _Err>990struct __expected_move_assign<void, _Err, false> : __expected_copy_assign<void, _Err>991{992  using __base = __expected_copy_assign<void, _Err>;993// nvbug3961621994#if defined(_LIBCUDACXX_COMPILER_NVRTC)  \995 || (defined(_LIBCUDACXX_CUDACC_BELOW_11_3) && defined(_LIBCUDACXX_COMPILER_CLANG))996  template<class... _Args, __enable_if_t<_LIBCUDACXX_TRAIT(is_constructible, __base, _Args...), int> = 0>997   _LIBCUDACXX_INLINE_VISIBILITY constexpr998  __expected_move_assign(_Args&&... __args) noexcept(noexcept(__base(_CUDA_VSTD::declval<_Args>()...)))999    : __base(_CUDA_VSTD::forward<_Args>(__args)...)1000  {}1001#else // ^^^ _LIBCUDACXX_COMPILER_NVRTC || nvcc < 11.3 ^^^ / vvv !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.3 vvv1002  using __base::__base;1003#endif // !_LIBCUDACXX_COMPILER_NVRTC || nvcc >= 11.31004 1005  __expected_move_assign() = default;1006  __expected_move_assign(const __expected_move_assign&) = default;1007  __expected_move_assign(__expected_move_assign&&) = default;1008  __expected_move_assign& operator=(const __expected_move_assign&) = default;1009 1010  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX171011  __expected_move_assign& operator=(__expected_move_assign&& __other)1012    noexcept(_LIBCUDACXX_TRAIT(is_nothrow_move_assignable, _Err) &&1013             _LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Err)) // strengthened1014  {1015    if (this->__has_val_ && __other.__has_val_) {1016      // nothing to do1017    } else if (this->__has_val_ && !__other.__has_val_) {1018      _LIBCUDACXX_CONSTRUCT_AT(this->__union_.__unex_, _CUDA_VSTD::move(__other.__union_.__unex_));1019      this->__has_val_ = false;1020    } else if (!this->__has_val_ && __other.__has_val_) {1021      _CUDA_VSTD::__destroy_at(_CUDA_VSTD::addressof(this->__union_.__unex_));1022      this->__has_val_ = true;1023    } else { // !this->__has_val_ && !__other.__has_val_1024      this->__union_.__unex_ = _CUDA_VSTD::move(__other.__union_.__unex_);1025    }1026    return *this;1027  }1028};1029 1030template <class _Err>1031using __expected_void_sfinae_ctor_base_t = __sfinae_ctor_base<1032  _LIBCUDACXX_TRAIT(is_copy_constructible, _Err),1033  _LIBCUDACXX_TRAIT(is_move_constructible, _Err)1034>;1035 1036template <class _Err>1037using __expected_void_sfinae_assign_base_t = __sfinae_assign_base<1038  _LIBCUDACXX_TRAIT(is_copy_constructible, _Err) &&1039  _LIBCUDACXX_TRAIT(is_copy_assignable, _Err),1040  _LIBCUDACXX_TRAIT(is_move_constructible, _Err) &&1041  _LIBCUDACXX_TRAIT(is_move_assignable, _Err)1042>;1043 1044_LIBCUDACXX_END_NAMESPACE_STD1045 1046#endif // _LIBCUDACXX_STD_VER > 111047 1048#endif // _LIBCUDACXX___EXPECTED_EXPECTED_BASE_H1049 
codekingpro/portable-devtools · Team Ai