Skip to content

Commit e356f68

Browse files
committed
[libc++] Implement std::expected P0323R12
Implement `std::expected` https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p0323r12.html Added tests Differential Revision: https://reviews.llvm.org/D124516 address comment
1 parent e6b9fc4 commit e356f68

File tree

125 files changed

+10419
-423
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+10419
-423
lines changed

libcxx/docs/FeatureTestMacroTable.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ Status
318318
------------------------------------------------- -----------------
319319
``__cpp_lib_constexpr_typeinfo`` *unimplemented*
320320
------------------------------------------------- -----------------
321+
``__cpp_lib_expected`` ``202202L``
322+
------------------------------------------------- -----------------
321323
``__cpp_lib_forward_like`` ``202207L``
322324
------------------------------------------------- -----------------
323325
``__cpp_lib_invoke_r`` *unimplemented*

libcxx/docs/Status/Cxx2bIssues.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@
167167
"`3671 <https://wg21.link/LWG3671>`__","``atomic_fetch_xor`` missing from ``stdatomic.h``","July 2022","",""
168168
"`3672 <https://wg21.link/LWG3672>`__","``common_iterator::operator->()`` should return by value","July 2022","","","|ranges|"
169169
"`3683 <https://wg21.link/LWG3683>`__","``operator==`` for ``polymorphic_allocator`` cannot deduce template argument in common cases","July 2022","",""
170-
"`3687 <https://wg21.link/LWG3687>`__","``expected<cv void, E>`` move constructor should move","July 2022","",""
170+
"`3687 <https://wg21.link/LWG3687>`__","``expected<cv void, E>`` move constructor should move","July 2022","Complete","16.0"
171171
"`3692 <https://wg21.link/LWG3692>`__","``zip_view::iterator``'s ``operator<=>`` is overconstrained","July 2022","","","|ranges| |spaceship|"
172172
"`3701 <https://wg21.link/LWG3701>`__","Make ``formatter<remove_cvref_t<const charT[N]>, charT>`` requirement explicit","July 2022","","","|format|"
173173
"`3702 <https://wg21.link/LWG3702>`__","Should ``zip_transform_view::iterator`` remove ``operator<``","July 2022","","","|ranges| |spaceship|"
174-
"`3703 <https://wg21.link/LWG3703>`__","Missing requirements for ``expected<T, E>`` requires ``is_void<T>``","July 2022","",""
174+
"`3703 <https://wg21.link/LWG3703>`__","Missing requirements for ``expected<T, E>`` requires ``is_void<T>``","July 2022","Complete","16.0"
175175
"`3704 <https://wg21.link/LWG3704>`__","LWG 2059 added overloads that might be ill-formed for sets","July 2022","",""
176176
"`3705 <https://wg21.link/LWG3705>`__","Hashability shouldn't depend on basic_string's allocator","July 2022","|Complete|","16.0"
177177
"`3707 <https://wg21.link/LWG3707>`__","chunk_view::outer-iterator::value_type::size should return unsigned type","July 2022","","","|ranges|"

libcxx/docs/Status/Cxx2bPapers.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"`P2393R1 <https://wg21.link/P2393R1>`__","LWG","Cleaning up ``integer``-class types","October 2021","",""
3939
"`P2401R0 <https://wg21.link/P2401R0>`__","LWG","Add a conditional ``noexcept`` specification to ``std::exchange``","October 2021","|Complete|","14.0"
4040
"","","","","",""
41-
"`P0323R12 <https://wg21.link/P0323R12>`__","LWG","``std::expected``","February 2022","",""
41+
"`P0323R12 <https://wg21.link/P0323R12>`__","LWG","``std::expected``","February 2022","|Complete|","16.0"
4242
"`P0533R9 <https://wg21.link/P0533R9>`__","LWG","``constexpr`` for ``<cmath>`` and ``<cstdlib>``","February 2022","",""
4343
"`P0627R6 <https://wg21.link/P0627R6>`__","LWG","Function to mark unreachable code","February 2022","|Complete|","15.0"
4444
"`P1206R7 <https://wg21.link/P1206R7>`__","LWG","``ranges::to``: A function to convert any range to a container","February 2022","","","|ranges|"
@@ -86,7 +86,7 @@
8686
"`P2517R1 <https://wg21.link/P2517R1>`__","LWG","Add a conditional ``noexcept`` specification to ``std::apply``","July 2022","",""
8787
"`P2520R0 <https://wg21.link/P2520R0>`__","LWG","``move_iterator`` should be a random access iterator","July 2022","","","|ranges|"
8888
"`P2540R1 <https://wg21.link/P2540R1>`__","LWG","Empty Product for certain Views","July 2022","","","|ranges|"
89-
"`P2549R1 <https://wg21.link/P2549R1>`__","LWG","``std::unexpected`` should have ``error()`` as member accessor","July 2022","",""
89+
"`P2549R1 <https://wg21.link/P2549R1>`__","LWG","``std::unexpected`` should have ``error()`` as member accessor","July 2022","|Complete|","16.0"
9090
"`P2553R1 <https://wg21.link/P2553R1>`__","LWG","Make ``mdspan`` ``size_type`` controllable","July 2022","",""
9191
"`P2554R0 <https://wg21.link/P2554R0>`__","LWG","C-Array Interoperability of MDSpan","July 2022","",""
9292
"`P2585R0 <https://wg21.link/P2585R0>`__","LWG","Improving default container formatting","July 2022","",""

libcxx/include/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ set(files
269269
__debug
270270
__debug_utils/randomize_range.h
271271
__errc
272+
__expected/bad_expected_access.h
273+
__expected/expected.h
274+
__expected/unexpect.h
275+
__expected/unexpected.h
272276
__filesystem/copy_options.h
273277
__filesystem/directory_entry.h
274278
__filesystem/directory_iterator.h
@@ -740,6 +744,7 @@ set(files
740744
errno.h
741745
exception
742746
execution
747+
expected
743748
experimental/__config
744749
experimental/__memory
745750
experimental/algorithm
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

0 commit comments

Comments
 (0)