1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 #ifndef _LIBCPP___EXPECTED_BAD_EXPECTED_ACCESS_H 10 #define _LIBCPP___EXPECTED_BAD_EXPECTED_ACCESS_H 11 12 #include <__config> 13 #include <__exception/exception.h> 14 #include <__utility/move.h> 15 16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 17 # pragma GCC system_header 18 #endif 19 20 #if _LIBCPP_STD_VER >= 23 21 22 _LIBCPP_BEGIN_NAMESPACE_STD 23 24 template <class _Err> 25 class bad_expected_access; 26 27 template <> 28 class bad_expected_access<void> : public exception { 29 protected: 30 _LIBCPP_HIDE_FROM_ABI bad_expected_access() noexcept = default; 31 _LIBCPP_HIDE_FROM_ABI bad_expected_access(const bad_expected_access&) = default; 32 _LIBCPP_HIDE_FROM_ABI bad_expected_access(bad_expected_access&&) = default; 33 _LIBCPP_HIDE_FROM_ABI bad_expected_access& operator=(const bad_expected_access&) = default; 34 _LIBCPP_HIDE_FROM_ABI bad_expected_access& operator=(bad_expected_access&&) = default; 35 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~bad_expected_access() override = default; 36 37 public: 38 // The way this has been designed (by using a class template below) means that we'll already 39 // have a profusion of these vtables in TUs, and the dynamic linker will already have a bunch 40 // of work to do. So it is not worth hiding the <void> specialization in the dylib, given that 41 // it adds deployment target restrictions. what()42 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const char* what() const noexcept override { return "bad access to std::expected"; } 43 }; 44 45 template <class _Err> 46 class bad_expected_access : public bad_expected_access<void> { 47 public: bad_expected_access(_Err __e)48 _LIBCPP_HIDE_FROM_ABI explicit bad_expected_access(_Err __e) : __unex_(std::move(__e)) {} 49 error()50 _LIBCPP_HIDE_FROM_ABI _Err& error() & noexcept { return __unex_; } error()51 _LIBCPP_HIDE_FROM_ABI const _Err& error() const& noexcept { return __unex_; } error()52 _LIBCPP_HIDE_FROM_ABI _Err&& error() && noexcept { return std::move(__unex_); } error()53 _LIBCPP_HIDE_FROM_ABI const _Err&& error() const&& noexcept { return std::move(__unex_); } 54 55 private: 56 _Err __unex_; 57 }; 58 59 _LIBCPP_END_NAMESPACE_STD 60 61 #endif // _LIBCPP_STD_VER >= 23 62 63 #endif // _LIBCPP___EXPECTED_BAD_EXPECTED_ACCESS_H 64