1 /* Documentation snippet
2 (C) 2017-2019 Niall Douglas <http://www.nedproductions.biz/> (7 commits)
3 File Created: Mar 2017
4 
5 
6 Boost Software License - Version 1.0 - August 17th, 2003
7 
8 Permission is hereby granted, free of charge, to any person or organization
9 obtaining a copy of the software and accompanying documentation covered by
10 this license (the "Software") to use, reproduce, display, distribute,
11 execute, and transmit the Software, and to prepare derivative works of the
12 Software, and to permit third-parties to whom the Software is furnished to
13 do so, all subject to the following:
14 
15 The copyright notices in the Software and this entire statement, including
16 the above license grant, this restriction and the following disclaimer,
17 must be included in all copies of the Software, in whole or in part, and
18 all derivative works of the Software, unless such copies or derivative
19 works are solely in the form of machine-executable object code generated by
20 a source language processor.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
25 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
26 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
27 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 DEALINGS IN THE SOFTWARE.
29 */
30 
31 #include "../../../include/boost/outcome/outcome.hpp"
32 
33 #if(_HAS_CXX17 || __cplusplus >= 201700)
34 //! [expected_implementation]
35 /* Here is a fairly conforming implementation of P0323R3 `expected<T, E>` using `checked<T, E>`.
36 It passes the reference test suite for P0323R3 at
37 https://github.com/viboes/std-make/blob/master/test/expected/expected_pass.cpp with modifications
38 only to move the test much closer to the P0323R3 Expected, as the reference test suite is for a
39 much older proposed Expected.
40 
41 Known differences from P0323R3 in this implementation:
42 - `T` and `E` cannot be the same type.
43 - `E` must be default constructible.
44 - No variant storage is implemented (note the Expected proposal does not actually require this).
45 */
46 
47 namespace detail
48 {
49   template <class T, class E> using expected_result = BOOST_OUTCOME_V2_NAMESPACE::checked<T, E>;
50   template <class T, class E> struct enable_default_constructor : public expected_result<T, E>
51   {
52     using base = expected_result<T, E>;
53     using base::base;
enable_default_constructordetail::enable_default_constructor54     constexpr enable_default_constructor()
55         : base{BOOST_OUTCOME_V2_NAMESPACE::in_place_type<T>}
56     {
57     }
58   };
59   template <class T, class E> using select_expected_base = std::conditional_t<std::is_default_constructible<T>::value, enable_default_constructor<T, E>, expected_result<T, E>>;
60 }
61 template <class T, class E> class expected : public detail::select_expected_base<T, E>
62 {
63   static_assert(!std::is_same<T, E>::value, "T and E cannot be the same in this expected implementation");
64   using base = detail::select_expected_base<T, E>;
65 
66 public:
67   // Inherit base's constructors
68   using base::base;
69   expected() = default;
70 
71   // Expected takes in_place not in_place_type
72   template <class... Args>
expected(std::in_place_t,Args &&...args)73   constexpr explicit expected(std::in_place_t /*unused*/, Args &&... args)
74       : base{BOOST_OUTCOME_V2_NAMESPACE::in_place_type<T>, std::forward<Args>(args)...}
75   {
76   }
77 
78   // Expected always accepts a T even if ambiguous
79   BOOST_OUTCOME_TEMPLATE(class U)
BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED (std::is_constructible<T,U>::value))80   BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_constructible<T, U>::value))
81   constexpr expected(U &&v)
82       : base{BOOST_OUTCOME_V2_NAMESPACE::in_place_type<T>, std::forward<U>(v)}
83   {
84   }
85 
86   // Expected has an emplace() modifier
emplace(Args &&...args)87   template <class... Args> void emplace(Args &&... args) { *static_cast<base *>(this) = base{BOOST_OUTCOME_V2_NAMESPACE::in_place_type<T>, std::forward<Args>(args)...}; }
88 
89   // Expected has a narrow operator* and operator->
operator *() const90   constexpr const T &operator*() const & { return base::assume_value(); }
operator *()91   constexpr T &operator*() & { return base::assume_value(); }
operator *() const92   constexpr const T &&operator*() const && { return base::assume_value(); }
operator *()93   constexpr T &&operator*() && { return base::assume_value(); }
operator ->() const94   constexpr const T *operator->() const { return &base::assume_value(); }
operator ->()95   constexpr T *operator->() { return &base::assume_value(); }
96 
97   // Expected has a narrow error() observer
error() const98   constexpr const E &error() const & { return base::assume_error(); }
error()99   constexpr E &error() & { return base::assume_error(); }
error() const100   constexpr const E &&error() const && { return base::assume_error(); }
error()101   constexpr E &error() && { return base::assume_error(); }
102 };
103 template <class E> class expected<void, E> : public BOOST_OUTCOME_V2_NAMESPACE::result<void, E, BOOST_OUTCOME_V2_NAMESPACE::policy::throw_bad_result_access<E, void>>
104 {
105   using base = BOOST_OUTCOME_V2_NAMESPACE::result<void, E, BOOST_OUTCOME_V2_NAMESPACE::policy::throw_bad_result_access<E, void>>;
106 
107 public:
108   // Inherit base constructors
109   using base::base;
110 
111   // Expected has a narrow operator* and operator->
operator *() const112   constexpr void operator*() const { base::assume_value(); }
operator ->() const113   constexpr void operator->() const { base::assume_value(); }
114 };
115 template <class E> using unexpected = BOOST_OUTCOME_V2_NAMESPACE::failure_type<E>;
make_unexpected(E && arg)116 template <class E> unexpected<E> make_unexpected(E &&arg)
117 {
118   return BOOST_OUTCOME_V2_NAMESPACE::failure<E>(std::forward<E>(arg));
119 }
make_unexpected(Args &&...args)120 template <class E, class... Args> unexpected<E> make_unexpected(Args &&... args)
121 {
122   return BOOST_OUTCOME_V2_NAMESPACE::failure<E>(std::forward<Args>(args)...);
123 }
124 template <class E> using bad_expected_access = BOOST_OUTCOME_V2_NAMESPACE::bad_result_access_with<E>;
125 //! [expected_implementation]
126 #endif
127 
main()128 int main()
129 {
130   return 0;
131 }
132