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 <__utility/move.h>
14 
15 #include <exception>
16 
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 #  pragma GCC system_header
19 #endif
20 
21 #if _LIBCPP_STD_VER >= 23
22 
23 _LIBCPP_BEGIN_NAMESPACE_STD
24 
25 template <class _Err>
26 class bad_expected_access;
27 
28 template <>
29 class bad_expected_access<void> : public exception {
30 protected:
31   _LIBCPP_HIDE_FROM_ABI bad_expected_access() noexcept                             = default;
32   _LIBCPP_HIDE_FROM_ABI bad_expected_access(const bad_expected_access&)            = default;
33   _LIBCPP_HIDE_FROM_ABI bad_expected_access(bad_expected_access&&)                 = default;
34   _LIBCPP_HIDE_FROM_ABI bad_expected_access& operator=(const bad_expected_access&) = default;
35   _LIBCPP_HIDE_FROM_ABI bad_expected_access& operator=(bad_expected_access&&)      = default;
36   ~bad_expected_access() override                                                  = default;
37 
38 public:
39   // The way this has been designed (by using a class template below) means that we'll already
40   // have a profusion of these vtables in TUs, and the dynamic linker will already have a bunch
41   // of work to do. So it is not worth hiding the <void> specialization in the dylib, given that
42   // it adds deployment target restrictions.
43   const char* what() const noexcept override { return "bad access to std::expected"; }
44 };
45 
46 template <class _Err>
47 class bad_expected_access : public bad_expected_access<void> {
48 public:
49   _LIBCPP_HIDE_FROM_ABI explicit bad_expected_access(_Err __e) : __unex_(std::move(__e)) {}
50 
51   _LIBCPP_HIDE_FROM_ABI _Err& error() & noexcept { return __unex_; }
52   _LIBCPP_HIDE_FROM_ABI const _Err& error() const& noexcept { return __unex_; }
53   _LIBCPP_HIDE_FROM_ABI _Err&& error() && noexcept { return std::move(__unex_); }
54   _LIBCPP_HIDE_FROM_ABI const _Err&& error() const&& noexcept { return std::move(__unex_); }
55 
56 private:
57   _Err __unex_;
58 };
59 
60 _LIBCPP_END_NAMESPACE_STD
61 
62 #endif // _LIBCPP_STD_VER >= 23
63 
64 #endif // _LIBCPP___EXPECTED_BAD_EXPECTED_ACCESS_H
65