2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Like folly::Optional, but can store a value *or* an error.
20 * @author Eric Niebler (eniebler@fb.com)
26 #include <initializer_list>
29 #include <type_traits>
32 #include <glog/logging.h>
34 #include <folly/Likely.h>
35 #include <folly/Optional.h>
36 #include <folly/Portability.h>
37 #include <folly/Preprocessor.h>
38 #include <folly/Traits.h>
39 #include <folly/Unit.h>
40 #include <folly/Utility.h>
42 #define FOLLY_EXPECTED_ID(X) FB_CONCATENATE(FB_CONCATENATE(Folly, X), __LINE__)
44 #define FOLLY_REQUIRES_IMPL(...) \
45 bool FOLLY_EXPECTED_ID(Requires) = false, \
46 typename std::enable_if< \
47 (FOLLY_EXPECTED_ID(Requires) || static_cast<bool>(__VA_ARGS__)), \
50 #define FOLLY_REQUIRES_TRAILING(...) , FOLLY_REQUIRES_IMPL(__VA_ARGS__)
52 #define FOLLY_REQUIRES(...) template <FOLLY_REQUIRES_IMPL(__VA_ARGS__)>
55 * gcc-4.7 warns about use of uninitialized memory around the use of storage_
56 * even though this is explicitly initialized at each point.
58 #if defined(__GNUC__) && !defined(__clang__)
59 #pragma GCC diagnostic push
60 #pragma GCC diagnostic ignored "-Wuninitialized"
61 #pragma GCC diagnostic ignored "-Wpragmas"
62 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
68 * Forward declarations
70 template <class Error>
73 template <class Error>
74 constexpr Unexpected<typename std::decay<Error>::type> makeUnexpected(Error&&);
76 template <class Value, class Error>
79 template <class Error, class Value>
80 constexpr Expected<typename std::decay<Value>::type, Error> makeExpected(
84 * Alias for an Expected type's assiciated value_type
86 template <class Expected>
87 using ExpectedValueType =
88 typename std::remove_reference<Expected>::type::value_type;
91 * Alias for an Expected type's assiciated error_type
93 template <class Expected>
94 using ExpectedErrorType =
95 typename std::remove_reference<Expected>::type::error_type;
98 namespace expected_detail {
100 template <typename Value, typename Error>
101 struct PromiseReturn;
104 // MSVC 2015 can't handle the StrictConjunction, so we have
105 // to use std::conjunction instead.
106 template <template <class...> class Trait, class... Ts>
107 using StrictAllOf = std::conjunction<Trait<Ts>...>;
109 template <template <class...> class Trait, class... Ts>
110 using StrictAllOf = StrictConjunction<Trait<Ts>...>;
114 using IsCopyable = StrictConjunction<
115 std::is_copy_constructible<T>,
116 std::is_copy_assignable<T>>;
119 using IsMovable = StrictConjunction<
120 std::is_move_constructible<T>,
121 std::is_move_assignable<T>>;
124 using IsNothrowCopyable = StrictConjunction<
125 std::is_nothrow_copy_constructible<T>,
126 std::is_nothrow_copy_assignable<T>>;
129 using IsNothrowMovable = StrictConjunction<
130 std::is_nothrow_move_constructible<T>,
131 std::is_nothrow_move_assignable<T>>;
133 template <class From, class To>
134 using IsConvertible = StrictConjunction<
135 std::is_constructible<To, From>,
136 std::is_assignable<To&, From>>;
138 template <class T, class U>
139 auto doEmplaceAssign(int, T& t, U&& u) -> decltype(void(t = (U &&)u)) {
143 template <class T, class U>
144 auto doEmplaceAssign(long, T& t, U&& u) -> decltype(void(T((U &&)u))) {
146 ::new ((void*)std::addressof(t)) T((U &&)u);
149 template <class T, class... Us>
150 auto doEmplaceAssign(int, T& t, Us&&... us)
151 -> decltype(void(t = T((Us &&)us...))) {
155 template <class T, class... Us>
156 auto doEmplaceAssign(long, T& t, Us&&... us)
157 -> decltype(void(T((Us &&)us...))) {
159 ::new ((void*)std::addressof(t)) T((Us &&)us...);
165 enum class Which : unsigned char { eEmpty, eValue, eError };
166 enum class StorageType { ePODStruct, ePODUnion, eUnion };
168 template <class Value, class Error>
169 constexpr StorageType getStorageType() {
170 return StrictAllOf<IsTriviallyCopyable, Value, Error>::value
171 ? (sizeof(std::pair<Value, Error>) <= sizeof(void * [2]) &&
172 StrictAllOf<std::is_trivial, Value, Error>::value
173 ? StorageType::ePODStruct
174 : StorageType::ePODUnion)
175 : StorageType::eUnion;
181 StorageType = expected_detail::getStorageType<Value, Error>()> // ePODUnion
182 struct ExpectedStorage {
183 using value_type = Value;
184 using error_type = Error;
192 template <class E = Error, class = decltype(E{})>
193 constexpr ExpectedStorage() noexcept(noexcept(E{}))
194 : error_{}, which_(Which::eError) {}
195 explicit constexpr ExpectedStorage(EmptyTag) noexcept
196 : ch_{}, which_(Which::eEmpty) {}
197 template <class... Vs>
198 explicit constexpr ExpectedStorage(ValueTag, Vs&&... vs) noexcept(
199 noexcept(Value(static_cast<Vs&&>(vs)...)))
200 : value_(static_cast<Vs&&>(vs)...), which_(Which::eValue) {}
201 template <class... Es>
202 explicit constexpr ExpectedStorage(ErrorTag, Es&&... es) noexcept(
203 noexcept(Error(static_cast<Es&&>(es)...)))
204 : error_(static_cast<Es&&>(es)...), which_(Which::eError) {}
205 void clear() noexcept {}
206 static constexpr bool uninitializedByException() noexcept {
207 // Although which_ may temporarily be eEmpty during construction, it
208 // is always either eValue or eError for a fully-constructed Expected.
211 template <class... Vs>
212 void assignValue(Vs&&... vs) {
213 expected_detail::doEmplaceAssign(0, value_, static_cast<Vs&&>(vs)...);
214 which_ = Which::eValue;
216 template <class... Es>
217 void assignError(Es&&... es) {
218 expected_detail::doEmplaceAssign(0, error_, static_cast<Es&&>(es)...);
219 which_ = Which::eError;
221 template <class Other>
222 void assign(Other&& that) {
223 switch (that.which_) {
225 this->assignValue(static_cast<Other&&>(that).value());
228 this->assignError(static_cast<Other&&>(that).error());
238 const Value& value() const& {
242 return std::move(value_);
244 // TODO (t17322426): remove when VS2015 support is deprecated
245 // VS2015 static analyzer incorrectly flags these as unreachable in certain
246 // circumstances. VS2017 does not have this problem on the same code.
248 FOLLY_MSVC_DISABLE_WARNING(4702) // unreachable code
252 const Error& error() const& {
256 return std::move(error_);
261 template <class Value, class Error>
262 struct ExpectedUnion {
268 Which which_ = Which::eEmpty;
270 explicit constexpr ExpectedUnion(EmptyTag) noexcept {}
271 template <class... Vs>
272 explicit constexpr ExpectedUnion(ValueTag, Vs&&... vs) noexcept(
273 noexcept(Value(static_cast<Vs&&>(vs)...)))
274 : value_(static_cast<Vs&&>(vs)...), which_(Which::eValue) {}
275 template <class... Es>
276 explicit constexpr ExpectedUnion(ErrorTag, Es&&... es) noexcept(
277 noexcept(Error(static_cast<Es&&>(es)...)))
278 : error_(static_cast<Es&&>(es)...), which_(Which::eError) {}
279 ExpectedUnion(const ExpectedUnion&) {}
280 ExpectedUnion(ExpectedUnion&&) noexcept {}
281 ExpectedUnion& operator=(const ExpectedUnion&) {
284 ExpectedUnion& operator=(ExpectedUnion&&) noexcept {
291 const Value& value() const& {
295 return std::move(value_);
300 const Error& error() const& {
304 return std::move(error_);
308 template <class Derived, bool, bool Noexcept>
309 struct CopyConstructible {
310 constexpr CopyConstructible() = default;
311 CopyConstructible(const CopyConstructible& that) noexcept(Noexcept) {
312 static_cast<Derived*>(this)->assign(static_cast<const Derived&>(that));
314 constexpr CopyConstructible(CopyConstructible&&) = default;
315 CopyConstructible& operator=(const CopyConstructible&) = default;
316 CopyConstructible& operator=(CopyConstructible&&) = default;
319 template <class Derived, bool Noexcept>
320 struct CopyConstructible<Derived, false, Noexcept> {
321 constexpr CopyConstructible() = default;
322 CopyConstructible(const CopyConstructible&) = delete;
323 constexpr CopyConstructible(CopyConstructible&&) = default;
324 CopyConstructible& operator=(const CopyConstructible&) = default;
325 CopyConstructible& operator=(CopyConstructible&&) = default;
328 template <class Derived, bool, bool Noexcept>
329 struct MoveConstructible {
330 constexpr MoveConstructible() = default;
331 constexpr MoveConstructible(const MoveConstructible&) = default;
332 MoveConstructible(MoveConstructible&& that) noexcept(Noexcept) {
333 static_cast<Derived*>(this)->assign(std::move(static_cast<Derived&>(that)));
335 MoveConstructible& operator=(const MoveConstructible&) = default;
336 MoveConstructible& operator=(MoveConstructible&&) = default;
339 template <class Derived, bool Noexcept>
340 struct MoveConstructible<Derived, false, Noexcept> {
341 constexpr MoveConstructible() = default;
342 constexpr MoveConstructible(const MoveConstructible&) = default;
343 MoveConstructible(MoveConstructible&&) = delete;
344 MoveConstructible& operator=(const MoveConstructible&) = default;
345 MoveConstructible& operator=(MoveConstructible&&) = default;
348 template <class Derived, bool, bool Noexcept>
349 struct CopyAssignable {
350 constexpr CopyAssignable() = default;
351 constexpr CopyAssignable(const CopyAssignable&) = default;
352 constexpr CopyAssignable(CopyAssignable&&) = default;
353 CopyAssignable& operator=(const CopyAssignable& that) noexcept(Noexcept) {
354 static_cast<Derived*>(this)->assign(static_cast<const Derived&>(that));
357 CopyAssignable& operator=(CopyAssignable&&) = default;
360 template <class Derived, bool Noexcept>
361 struct CopyAssignable<Derived, false, Noexcept> {
362 constexpr CopyAssignable() = default;
363 constexpr CopyAssignable(const CopyAssignable&) = default;
364 constexpr CopyAssignable(CopyAssignable&&) = default;
365 CopyAssignable& operator=(const CopyAssignable&) = delete;
366 CopyAssignable& operator=(CopyAssignable&&) = default;
369 template <class Derived, bool, bool Noexcept>
370 struct MoveAssignable {
371 constexpr MoveAssignable() = default;
372 constexpr MoveAssignable(const MoveAssignable&) = default;
373 constexpr MoveAssignable(MoveAssignable&&) = default;
374 MoveAssignable& operator=(const MoveAssignable&) = default;
375 MoveAssignable& operator=(MoveAssignable&& that) noexcept(Noexcept) {
376 static_cast<Derived*>(this)->assign(std::move(static_cast<Derived&>(that)));
381 template <class Derived, bool Noexcept>
382 struct MoveAssignable<Derived, false, Noexcept> {
383 constexpr MoveAssignable() = default;
384 constexpr MoveAssignable(const MoveAssignable&) = default;
385 constexpr MoveAssignable(MoveAssignable&&) = default;
386 MoveAssignable& operator=(const MoveAssignable&) = default;
387 MoveAssignable& operator=(MoveAssignable&& that) = delete;
390 template <class Value, class Error>
391 struct ExpectedStorage<Value, Error, StorageType::eUnion>
392 : ExpectedUnion<Value, Error>,
394 ExpectedStorage<Value, Error, StorageType::eUnion>,
395 StrictAllOf<std::is_copy_constructible, Value, Error>::value,
396 StrictAllOf<std::is_nothrow_copy_constructible, Value, Error>::value>,
398 ExpectedStorage<Value, Error, StorageType::eUnion>,
399 StrictAllOf<std::is_move_constructible, Value, Error>::value,
400 StrictAllOf<std::is_nothrow_move_constructible, Value, Error>::value>,
402 ExpectedStorage<Value, Error, StorageType::eUnion>,
403 StrictAllOf<IsCopyable, Value, Error>::value,
404 StrictAllOf<IsNothrowCopyable, Value, Error>::value>,
406 ExpectedStorage<Value, Error, StorageType::eUnion>,
407 StrictAllOf<IsMovable, Value, Error>::value,
408 StrictAllOf<IsNothrowMovable, Value, Error>::value> {
409 using value_type = Value;
410 using error_type = Error;
411 using Base = ExpectedUnion<Value, Error>;
412 template <class E = Error, class = decltype(E{})>
413 constexpr ExpectedStorage() noexcept(noexcept(E{})) : Base{ErrorTag{}} {}
414 ExpectedStorage(const ExpectedStorage&) = default;
415 ExpectedStorage(ExpectedStorage&&) = default;
416 ExpectedStorage& operator=(const ExpectedStorage&) = default;
417 ExpectedStorage& operator=(ExpectedStorage&&) = default;
418 using ExpectedUnion<Value, Error>::ExpectedUnion;
422 void clear() noexcept {
423 switch (this->which_) {
425 this->value().~Value();
428 this->error().~Error();
433 this->which_ = Which::eEmpty;
435 bool uninitializedByException() const noexcept {
436 return this->which_ == Which::eEmpty;
438 template <class... Vs>
439 void assignValue(Vs&&... vs) {
440 if (this->which_ == Which::eValue) {
441 expected_detail::doEmplaceAssign(
442 0, this->value(), static_cast<Vs&&>(vs)...);
445 ::new ((void*)std::addressof(this->value()))
446 Value(static_cast<Vs&&>(vs)...);
447 this->which_ = Which::eValue;
450 template <class... Es>
451 void assignError(Es&&... es) {
452 if (this->which_ == Which::eError) {
453 expected_detail::doEmplaceAssign(
454 0, this->error(), static_cast<Es&&>(es)...);
457 ::new ((void*)std::addressof(this->error()))
458 Error(static_cast<Es&&>(es)...);
459 this->which_ = Which::eError;
462 bool isSelfAssign(const ExpectedStorage* that) const {
465 constexpr bool isSelfAssign(const void*) const {
468 template <class Other>
469 void assign(Other&& that) {
470 if (isSelfAssign(&that)) {
473 switch (that.which_) {
475 this->assignValue(static_cast<Other&&>(that).value());
478 this->assignError(static_cast<Other&&>(that).error());
487 // For small (pointer-sized) trivial types, a struct is faster than a union.
488 template <class Value, class Error>
489 struct ExpectedStorage<Value, Error, StorageType::ePODStruct> {
490 using value_type = Value;
491 using error_type = Error;
496 constexpr ExpectedStorage() noexcept
497 : which_(Which::eError), error_{}, value_{} {}
498 explicit constexpr ExpectedStorage(EmptyTag) noexcept
499 : which_(Which::eEmpty), error_{}, value_{} {}
500 template <class... Vs>
501 explicit constexpr ExpectedStorage(ValueTag, Vs&&... vs) noexcept(
502 noexcept(Value(static_cast<Vs&&>(vs)...)))
503 : which_(Which::eValue), error_{}, value_(static_cast<Vs&&>(vs)...) {}
504 template <class... Es>
505 explicit constexpr ExpectedStorage(ErrorTag, Es&&... es) noexcept(
506 noexcept(Error(static_cast<Es&&>(es)...)))
507 : which_(Which::eError), error_(static_cast<Es&&>(es)...), value_{} {}
508 void clear() noexcept {}
509 constexpr static bool uninitializedByException() noexcept {
512 template <class... Vs>
513 void assignValue(Vs&&... vs) {
514 expected_detail::doEmplaceAssign(0, value_, static_cast<Vs&&>(vs)...);
515 which_ = Which::eValue;
517 template <class... Es>
518 void assignError(Es&&... es) {
519 expected_detail::doEmplaceAssign(0, error_, static_cast<Es&&>(es)...);
520 which_ = Which::eError;
522 template <class Other>
523 void assign(Other&& that) {
524 switch (that.which_) {
526 this->assignValue(static_cast<Other&&>(that).value());
529 this->assignError(static_cast<Other&&>(that).error());
539 const Value& value() const& {
543 return std::move(value_);
545 // TODO (t17322426): remove when VS2015 support is deprecated
546 // VS2015 static analyzer incorrectly flags these as unreachable in certain
547 // circumstances. VS2017 does not have this problem on the same code.
549 FOLLY_MSVC_DISABLE_WARNING(4702) // unreachable code
553 const Error& error() const& {
557 return std::move(error_);
562 namespace expected_detail_ExpectedHelper {
563 // Tricky hack so that Expected::then can handle lambdas that return void
565 inline T&& operator,(T&& t, Unit) noexcept {
566 return static_cast<T&&>(t);
569 struct ExpectedHelper {
570 template <class Error, class T>
571 static constexpr Expected<T, Error> return_(T t) {
572 return folly::makeExpected<Error>(t);
577 class U FOLLY_REQUIRES_TRAILING(
578 expected_detail::IsConvertible<U&&, Error>::value)>
579 static constexpr Expected<T, Error> return_(Expected<T, U> t) {
583 template <class This>
584 static typename std::decay<This>::type then_(This&& ex) {
585 return static_cast<This&&>(ex);
589 // Don't warn about not using the overloaded comma operator.
590 FOLLY_MSVC_DISABLE_WARNING(4913)
595 class E = ExpectedErrorType<This>,
596 class T = ExpectedHelper>
597 static auto then_(This&& ex, Fn&& fn, Fns&&... fns) -> decltype(T::then_(
598 T::template return_<E>(
599 (std::declval<Fn>()(std::declval<This>().value()), unit)),
600 std::declval<Fns>()...)) {
601 if (LIKELY(ex.which_ == expected_detail::Which::eValue)) {
603 T::template return_<E>(
604 // Uses the comma operator defined above IFF the lambda
606 (static_cast<Fn&&>(fn)(static_cast<This&&>(ex).value()), unit)),
607 static_cast<Fns&&>(fns)...);
609 return makeUnexpected(static_cast<This&&>(ex).error());
616 class Ret = decltype(std::declval<Yes>()(std::declval<This>().value())),
617 class Err = decltype(std::declval<No>()(std::declval<This>().error()))
618 FOLLY_REQUIRES_TRAILING(!std::is_void<Err>::value)>
619 static Ret thenOrThrow_(This&& ex, Yes&& yes, No&& no) {
620 if (LIKELY(ex.which_ == expected_detail::Which::eValue)) {
621 return Ret(static_cast<Yes&&>(yes)(static_cast<This&&>(ex).value()));
623 throw static_cast<No&&>(no)(static_cast<This&&>(ex).error());
630 class Ret = decltype(std::declval<Yes>()(std::declval<This>().value())),
631 class Err = decltype(std::declval<No>()(std::declval<This&>().error()))
632 FOLLY_REQUIRES_TRAILING(std::is_void<Err>::value)>
633 static Ret thenOrThrow_(This&& ex, Yes&& yes, No&& no) {
634 if (LIKELY(ex.which_ == expected_detail::Which::eValue)) {
635 return Ret(static_cast<Yes&&>(yes)(static_cast<This&&>(ex).value()));
637 static_cast<No&&>(no)(ex.error());
638 throw typename Unexpected<ExpectedErrorType<This>>::MakeBadExpectedAccess()(
639 static_cast<This&&>(ex).error());
643 } // namespace expected_detail_ExpectedHelper
644 /* using override */ using expected_detail_ExpectedHelper::ExpectedHelper;
646 struct UnexpectedTag {};
648 } // namespace expected_detail
651 expected_detail::UnexpectedTag (&)(expected_detail::UnexpectedTag);
653 inline expected_detail::UnexpectedTag unexpected(
654 expected_detail::UnexpectedTag = {}) {
659 * An exception type thrown by Expected on catastrophic logic errors.
661 class BadExpectedAccess : public std::logic_error {
663 BadExpectedAccess() : std::logic_error("bad Expected access") {}
667 * Unexpected - a helper type used to disambiguate the construction of
668 * Expected objects in the error state.
670 template <class Error>
671 class Unexpected final {
673 friend class Unexpected;
674 template <class V, class E>
675 friend class Expected;
676 friend struct expected_detail::ExpectedHelper;
680 * Unexpected::BadExpectedAccess - An exception type thrown by Expected
681 * when the user tries to access the nested value but the Expected object is
682 * actually storing an error code.
684 class BadExpectedAccess : public folly::BadExpectedAccess {
686 explicit BadExpectedAccess(Error err)
687 : folly::BadExpectedAccess{}, error_(std::move(err)) {}
689 * The error code that was held by the Expected object when the user
690 * erroneously requested the value.
692 Error error() const {
703 Unexpected() = default;
704 Unexpected(const Unexpected&) = default;
705 Unexpected(Unexpected&&) = default;
706 Unexpected& operator=(const Unexpected&) = default;
707 Unexpected& operator=(Unexpected&&) = default;
708 constexpr /* implicit */ Unexpected(const Error& err) : error_(err) {}
709 constexpr /* implicit */ Unexpected(Error&& err) : error_(std::move(err)) {}
711 template <class Other FOLLY_REQUIRES_TRAILING(
712 std::is_constructible<Error, Other&&>::value)>
713 constexpr /* implicit */ Unexpected(Unexpected<Other> that)
714 : error_(std::move(that.error())) {}
719 template <class Other FOLLY_REQUIRES_TRAILING(
720 std::is_assignable<Error&, Other&&>::value)>
721 Unexpected& operator=(Unexpected<Other> that) {
722 error_ = std::move(that.error());
731 const Error& error() const& {
735 return std::move(error_);
739 struct MakeBadExpectedAccess {
741 BadExpectedAccess operator()(E&& err) const {
742 return BadExpectedAccess(static_cast<E&&>(err));
750 class Error FOLLY_REQUIRES_TRAILING(IsEqualityComparable<Error>::value)>
751 inline bool operator==(
752 const Unexpected<Error>& lhs,
753 const Unexpected<Error>& rhs) {
754 return lhs.error() == rhs.error();
758 class Error FOLLY_REQUIRES_TRAILING(IsEqualityComparable<Error>::value)>
759 inline bool operator!=(
760 const Unexpected<Error>& lhs,
761 const Unexpected<Error>& rhs) {
762 return !(lhs == rhs);
766 * For constructing an Unexpected object from an error code. Unexpected objects
767 * are implicitly convertible to Expected object in the error state. Usage is
770 * enum class MyErrorCode { BAD_ERROR, WORSE_ERROR };
771 * Expected<int, MyErrorCode> myAPI() {
773 * return i ? makeExpected<MyErrorCode>(i)
774 * : makeUnexpected(MyErrorCode::BAD_ERROR);
777 template <class Error>
778 constexpr Unexpected<typename std::decay<Error>::type> makeUnexpected(
780 return Unexpected<typename std::decay<Error>::type>{
781 static_cast<Error&&>(err)};
785 * Expected - For holding a value or an error. Useful as an alternative to
786 * exceptions, for APIs where throwing on failure would be too expensive.
788 * Expected<Value, Error> is a variant over the types Value and Error.
790 * Expected does not offer support for references. Use
791 * Expected<std::reference_wrapper<T>, Error> if your API needs to return a
792 * reference or an error.
794 * Expected offers a continuation-based interface to reduce the boilerplate
795 * of checking error codes. The Expected::then member function takes a lambda
796 * that is to execute should the Expected object contain a value. The return
797 * value of the lambda is wrapped in an Expected and returned. If the lambda is
798 * not executed because the Expected contains an error, the error is returned
799 * immediately in a new Expected object.
801 * Expected<int, Error> funcTheFirst();
802 * Expected<std::string, Error> funcTheSecond() {
803 * return funcTheFirst().then([](int i) { return std::to_string(i); });
806 * The above line of code could more verbosely written as:
808 * Expected<std::string, Error> funcTheSecond() {
809 * if (auto ex = funcTheFirst()) {
810 * return std::to_string(*ex);
812 * return makeUnexpected(ex.error());
815 * Continuations can chain, like:
817 * Expected<D, Error> maybeD = someFunc()
818 * .then([](A a){return B(a);})
819 * .then([](B b){return C(b);})
820 * .then([](C c){return D(c);});
822 * To avoid the redundant error checking that would happen if a call at the
823 * front of the chain returns an error, these call chains can be collaped into
824 * a single call to .then:
826 * Expected<D, Error> maybeD = someFunc()
827 * .then([](A a){return B(a);},
828 * [](B b){return C(b);},
829 * [](C c){return D(c);});
831 * The result of .then() is wrapped into Expected< ~, Error > if it isn't
832 * of that form already. Consider the following code:
834 * extern Expected<std::string, Error> readLineFromIO();
835 * extern Expected<int, Error> parseInt(std::string);
836 * extern int increment(int);
838 * Expected<int, Error> x = readLineFromIO().then(parseInt).then(increment);
840 * From the code above, we see that .then() works both with functions that
841 * return an Expected< ~, Error > (like parseInt) and with ones that return
842 * a plain value (like increment). In the case of parseInt, .then() returns
843 * the result of parseInt as-is. In the case of increment, it wraps the int
844 * that increment returns into an Expected< int, Error >.
846 * Sometimes when using a continuation you would prefer an exception to be
847 * thrown for a value-less Expected. For that you can use .thenOrThrow, as
851 * .thenOrThrow([](A a){return B(a);});
853 * The above call to thenOrThrow will invoke the lambda if the Expected returned
854 * by someFunc() contains a value. Otherwise, it will throw an exception of type
855 * Unexpected<Error>::BadExpectedAccess. If you prefer it throw an exception of
856 * a different type, you can pass a second lambda to thenOrThrow:
859 * .thenOrThrow([](A a){return B(a);},
860 * [](Error e) {throw MyException(e);});
862 * Like C++17's std::variant, Expected offers the almost-never-empty guarantee;
863 * that is, an Expected<Value, Error> almost always contains either a Value or
864 * and Error. Partially-formed Expected objects occur when an assignment to
865 * an Expected object that would change the type of the contained object (Value-
866 * to-Error or vice versa) throws. Trying to access either the contained value
867 * or error object causes Expected to throw folly::BadExpectedAccess.
869 * Expected models OptionalPointee, so calling 'get_pointer(ex)' will return a
870 * pointer to nullptr if the 'ex' is in the error state, and a pointer to the
873 * Expected<int, Error> maybeInt = ...;
874 * if (int* v = get_pointer(maybeInt)) {
875 * cout << *v << endl;
878 template <class Value, class Error>
879 class Expected final : expected_detail::ExpectedStorage<Value, Error> {
880 template <class, class>
881 friend class Expected;
882 template <class, class, expected_detail::StorageType>
883 friend struct expected_detail::ExpectedStorage;
884 friend struct expected_detail::ExpectedHelper;
885 using Base = expected_detail::ExpectedStorage<Value, Error>;
886 using MakeBadExpectedAccess =
887 typename Unexpected<Error>::MakeBadExpectedAccess;
891 const Base& base() const& {
895 return std::move(*this);
899 using value_type = Value;
900 using error_type = Error;
901 using IsTriviallyCopyable = typename expected_detail::
902 StrictAllOf<IsTriviallyCopyable, Value, Error>::type;
905 using rebind = Expected<U, Error>;
908 !std::is_reference<Value>::value,
909 "Expected may not be used with reference types");
911 !std::is_abstract<Value>::value,
912 "Expected may not be used with abstract types");
917 template <class B = Base, class = decltype(B{})>
918 Expected() noexcept(noexcept(B{})) : Base{} {}
919 Expected(const Expected& that) = default;
920 Expected(Expected&& that) = default;
924 class E FOLLY_REQUIRES_TRAILING(
925 !std::is_same<Expected<V, E>, Expected>::value &&
926 std::is_constructible<Value, V&&>::value &&
927 std::is_constructible<Error, E&&>::value)>
928 Expected(Expected<V, E> that) : Base{expected_detail::EmptyTag{}} {
929 *this = std::move(that);
932 FOLLY_REQUIRES(std::is_copy_constructible<Value>::value)
933 constexpr /* implicit */ Expected(const Value& val) noexcept(
934 noexcept(Value(val)))
935 : Base{expected_detail::ValueTag{}, val} {}
937 FOLLY_REQUIRES(std::is_move_constructible<Value>::value)
938 constexpr /* implicit */ Expected(Value&& val) noexcept(
939 noexcept(Value(std::move(val))))
940 : Base{expected_detail::ValueTag{}, std::move(val)} {}
942 template <class T FOLLY_REQUIRES_TRAILING(
943 std::is_convertible<T, Value>::value &&
944 !std::is_convertible<T, Error>::value)>
945 constexpr /* implicit */ Expected(T&& val) noexcept(
946 noexcept(Value(static_cast<T&&>(val))))
947 : Base{expected_detail::ValueTag{}, static_cast<T&&>(val)} {}
949 template <class... Ts FOLLY_REQUIRES_TRAILING(
950 std::is_constructible<Value, Ts&&...>::value)>
951 explicit constexpr Expected(in_place_t, Ts&&... ts) noexcept(
952 noexcept(Value(std::declval<Ts>()...)))
953 : Base{expected_detail::ValueTag{}, static_cast<Ts&&>(ts)...} {}
957 class... Ts FOLLY_REQUIRES_TRAILING(
958 std::is_constructible<Value, std::initializer_list<U>&, Ts&&...>::
960 explicit constexpr Expected(
962 std::initializer_list<U> il,
963 Ts&&... ts) noexcept(noexcept(Value(std::declval<Ts>()...)))
964 : Base{expected_detail::ValueTag{}, il, static_cast<Ts&&>(ts)...} {}
966 // If overload resolution selects one of these deleted functions, that
967 // means you need to use makeUnexpected
968 /* implicit */ Expected(const Error&) = delete;
969 /* implicit */ Expected(Error&&) = delete;
971 FOLLY_REQUIRES(std::is_copy_constructible<Error>::value)
972 constexpr Expected(unexpected_t, const Error& err) noexcept(
973 noexcept(Error(err)))
974 : Base{expected_detail::ErrorTag{}, err} {}
976 FOLLY_REQUIRES(std::is_move_constructible<Error>::value)
977 constexpr Expected(unexpected_t, Error&& err) noexcept(
978 noexcept(Error(std::move(err))))
979 : Base{expected_detail::ErrorTag{}, std::move(err)} {}
981 FOLLY_REQUIRES(std::is_copy_constructible<Error>::value)
982 constexpr /* implicit */ Expected(const Unexpected<Error>& err) noexcept(
983 noexcept(Error(err.error())))
984 : Base{expected_detail::ErrorTag{}, err.error()} {}
986 FOLLY_REQUIRES(std::is_move_constructible<Error>::value)
987 constexpr /* implicit */ Expected(Unexpected<Error>&& err) noexcept(
988 noexcept(Error(std::move(err.error()))))
989 : Base{expected_detail::ErrorTag{}, std::move(err.error())} {}
992 * Assignment operators
994 Expected& operator=(const Expected& that) = default;
995 Expected& operator=(Expected&& that) = default;
999 class E FOLLY_REQUIRES_TRAILING(
1000 !std::is_same<Expected<V, E>, Expected>::value &&
1001 expected_detail::IsConvertible<V&&, Value>::value &&
1002 expected_detail::IsConvertible<E&&, Error>::value)>
1003 Expected& operator=(Expected<V, E> that) {
1004 this->assign(std::move(that));
1008 FOLLY_REQUIRES(expected_detail::IsCopyable<Value>::value)
1009 Expected& operator=(const Value& val) noexcept(
1010 expected_detail::IsNothrowCopyable<Value>::value) {
1011 this->assignValue(val);
1015 FOLLY_REQUIRES(expected_detail::IsMovable<Value>::value)
1016 Expected& operator=(Value&& val) noexcept(
1017 expected_detail::IsNothrowMovable<Value>::value) {
1018 this->assignValue(std::move(val));
1022 template <class T FOLLY_REQUIRES_TRAILING(
1023 std::is_convertible<T, Value>::value &&
1024 !std::is_convertible<T, Error>::value)>
1025 Expected& operator=(T&& val) {
1026 this->assignValue(static_cast<T&&>(val));
1030 FOLLY_REQUIRES(expected_detail::IsCopyable<Error>::value)
1031 Expected& operator=(const Unexpected<Error>& err) noexcept(
1032 expected_detail::IsNothrowCopyable<Error>::value) {
1033 this->assignError(err.error());
1037 FOLLY_REQUIRES(expected_detail::IsMovable<Error>::value)
1038 Expected& operator=(Unexpected<Error>&& err) noexcept(
1039 expected_detail::IsNothrowMovable<Error>::value) {
1040 this->assignError(std::move(err.error()));
1044 // Used only when an Expected is used with coroutines on MSVC
1045 /* implicit */ Expected(const expected_detail::PromiseReturn<Value, Error>& p)
1047 p.promise_->value_ = this;
1050 template <class... Ts FOLLY_REQUIRES_TRAILING(
1051 std::is_constructible<Value, Ts&&...>::value)>
1052 void emplace(Ts&&... ts) {
1053 this->assignValue(static_cast<Ts&&>(ts)...);
1059 void swap(Expected& that) noexcept(
1060 expected_detail::StrictAllOf<IsNothrowSwappable, Value, Error>::value) {
1061 if (this->uninitializedByException() || that.uninitializedByException()) {
1062 throw BadExpectedAccess();
1067 swap(this->value_, that.value_);
1069 Error e(std::move(that.error_));
1070 that.assignValue(std::move(this->value_));
1071 this->assignError(std::move(e));
1075 swap(this->error_, that.error_);
1077 Error e(std::move(this->error_));
1078 this->assignValue(std::move(that.value_));
1079 that.assignError(std::move(e));
1084 // If overload resolution selects one of these deleted functions, that
1085 // means you need to use makeUnexpected
1086 /* implicit */ Expected& operator=(const Error&) = delete;
1087 /* implicit */ Expected& operator=(Error&&) = delete;
1090 * Relational Operators
1092 template <class Val, class Err>
1093 friend typename std::enable_if<IsEqualityComparable<Val>::value, bool>::type
1094 operator==(const Expected<Val, Err>& lhs, const Expected<Val, Err>& rhs);
1095 template <class Val, class Err>
1096 friend typename std::enable_if<IsLessThanComparable<Val>::value, bool>::type
1097 operator<(const Expected<Val, Err>& lhs, const Expected<Val, Err>& rhs);
1102 constexpr bool hasValue() const noexcept {
1103 return expected_detail::Which::eValue == this->which_;
1106 constexpr bool hasError() const noexcept {
1107 return expected_detail::Which::eError == this->which_;
1110 using Base::uninitializedByException;
1112 const Value& value() const& {
1114 return this->Base::value();
1119 return this->Base::value();
1122 Value&& value() && {
1124 return std::move(this->Base::value());
1127 const Error& error() const& {
1129 return this->Base::error();
1134 return this->Base::error();
1137 Error&& error() && {
1139 return std::move(this->Base::error());
1142 // Return a copy of the value if set, or a given default if not.
1144 Value value_or(U&& dflt) const& {
1145 if (LIKELY(this->which_ == expected_detail::Which::eValue)) {
1146 return this->value_;
1148 return static_cast<U&&>(dflt);
1152 Value value_or(U&& dflt) && {
1153 if (LIKELY(this->which_ == expected_detail::Which::eValue)) {
1154 return std::move(this->value_);
1156 return static_cast<U&&>(dflt);
1159 explicit constexpr operator bool() const noexcept {
1163 const Value& operator*() const& {
1164 return this->value();
1167 Value& operator*() & {
1168 return this->value();
1171 Value&& operator*() && {
1172 return std::move(this->value());
1175 const Value* operator->() const {
1176 return std::addressof(this->value());
1179 Value* operator->() {
1180 return std::addressof(this->value());
1183 const Value* get_pointer() const& noexcept {
1184 return hasValue() ? std::addressof(this->value_) : nullptr;
1187 Value* get_pointer() & noexcept {
1188 return hasValue() ? std::addressof(this->value_) : nullptr;
1191 Value* get_pointer() && = delete;
1196 template <class... Fns FOLLY_REQUIRES_TRAILING(sizeof...(Fns) >= 1)>
1197 auto then(Fns&&... fns) const& -> decltype(
1198 expected_detail::ExpectedHelper::then_(
1199 std::declval<const Base&>(),
1200 std::declval<Fns>()...)) {
1201 if (this->uninitializedByException()) {
1202 throw BadExpectedAccess();
1204 return expected_detail::ExpectedHelper::then_(
1205 base(), static_cast<Fns&&>(fns)...);
1208 template <class... Fns FOLLY_REQUIRES_TRAILING(sizeof...(Fns) >= 1)>
1209 auto then(Fns&&... fns) & -> decltype(expected_detail::ExpectedHelper::then_(
1210 std::declval<Base&>(),
1211 std::declval<Fns>()...)) {
1212 if (this->uninitializedByException()) {
1213 throw BadExpectedAccess();
1215 return expected_detail::ExpectedHelper::then_(
1216 base(), static_cast<Fns&&>(fns)...);
1219 template <class... Fns FOLLY_REQUIRES_TRAILING(sizeof...(Fns) >= 1)>
1220 auto then(Fns&&... fns) && -> decltype(expected_detail::ExpectedHelper::then_(
1221 std::declval<Base&&>(),
1222 std::declval<Fns>()...)) {
1223 if (this->uninitializedByException()) {
1224 throw BadExpectedAccess();
1226 return expected_detail::ExpectedHelper::then_(
1227 std::move(base()), static_cast<Fns&&>(fns)...);
1233 template <class Yes, class No = MakeBadExpectedAccess>
1234 auto thenOrThrow(Yes&& yes, No&& no = No{}) const& -> decltype(
1235 std::declval<Yes>()(std::declval<const Value&>())) {
1236 using Ret = decltype(std::declval<Yes>()(std::declval<const Value&>()));
1237 if (this->uninitializedByException()) {
1238 throw BadExpectedAccess();
1240 return Ret(expected_detail::ExpectedHelper::thenOrThrow_(
1241 base(), static_cast<Yes&&>(yes), static_cast<No&&>(no)));
1244 template <class Yes, class No = MakeBadExpectedAccess>
1245 auto thenOrThrow(Yes&& yes, No&& no = No{}) & -> decltype(
1246 std::declval<Yes>()(std::declval<Value&>())) {
1247 using Ret = decltype(std::declval<Yes>()(std::declval<Value&>()));
1248 if (this->uninitializedByException()) {
1249 throw BadExpectedAccess();
1251 return Ret(expected_detail::ExpectedHelper::thenOrThrow_(
1252 base(), static_cast<Yes&&>(yes), static_cast<No&&>(no)));
1255 template <class Yes, class No = MakeBadExpectedAccess>
1256 auto thenOrThrow(Yes&& yes, No&& no = No{}) && -> decltype(
1257 std::declval<Yes>()(std::declval<Value&&>())) {
1258 using Ret = decltype(std::declval<Yes>()(std::declval<Value&&>()));
1259 if (this->uninitializedByException()) {
1260 throw BadExpectedAccess();
1262 return Ret(expected_detail::ExpectedHelper::thenOrThrow_(
1263 std::move(base()), static_cast<Yes&&>(yes), static_cast<No&&>(no)));
1267 void requireValue() const {
1268 if (UNLIKELY(!hasValue())) {
1269 if (LIKELY(hasError())) {
1270 throw typename Unexpected<Error>::BadExpectedAccess(this->error_);
1272 throw BadExpectedAccess();
1276 void requireError() const {
1277 if (UNLIKELY(!hasError())) {
1278 throw BadExpectedAccess();
1282 expected_detail::Which which() const noexcept {
1283 return this->which_;
1287 template <class Value, class Error>
1288 inline typename std::enable_if<IsEqualityComparable<Value>::value, bool>::type
1290 const Expected<Value, Error>& lhs,
1291 const Expected<Value, Error>& rhs) {
1292 if (UNLIKELY(lhs.which_ != rhs.which_)) {
1293 return UNLIKELY(lhs.uninitializedByException()) ? false
1294 : throw BadExpectedAccess();
1296 if (UNLIKELY(lhs.uninitializedByException())) {
1297 throw BadExpectedAccess();
1299 if (UNLIKELY(lhs.hasError())) {
1300 return true; // All error states are considered equal
1302 return lhs.value_ == rhs.value_;
1307 class Error FOLLY_REQUIRES_TRAILING(IsEqualityComparable<Value>::value)>
1308 inline bool operator!=(
1309 const Expected<Value, Error>& lhs,
1310 const Expected<Value, Error>& rhs) {
1311 return !(rhs == lhs);
1314 template <class Value, class Error>
1315 inline typename std::enable_if<IsLessThanComparable<Value>::value, bool>::type
1317 const Expected<Value, Error>& lhs,
1318 const Expected<Value, Error>& rhs) {
1320 lhs.uninitializedByException() || rhs.uninitializedByException())) {
1321 throw BadExpectedAccess();
1323 if (UNLIKELY(lhs.hasError())) {
1324 return !rhs.hasError();
1326 if (UNLIKELY(rhs.hasError())) {
1329 return lhs.value_ < rhs.value_;
1334 class Error FOLLY_REQUIRES_TRAILING(IsLessThanComparable<Value>::value)>
1335 inline bool operator<=(
1336 const Expected<Value, Error>& lhs,
1337 const Expected<Value, Error>& rhs) {
1338 return !(rhs < lhs);
1343 class Error FOLLY_REQUIRES_TRAILING(IsLessThanComparable<Value>::value)>
1344 inline bool operator>(
1345 const Expected<Value, Error>& lhs,
1346 const Expected<Value, Error>& rhs) {
1352 class Error FOLLY_REQUIRES_TRAILING(IsLessThanComparable<Value>::value)>
1353 inline bool operator>=(
1354 const Expected<Value, Error>& lhs,
1355 const Expected<Value, Error>& rhs) {
1356 return !(lhs < rhs);
1360 * swap Expected values
1362 template <class Error, class Value>
1363 void swap(Expected<Error, Value>& lhs, Expected<Value, Error>& rhs) noexcept(
1364 expected_detail::StrictAllOf<IsNothrowSwappable, Value, Error>::value) {
1368 template <class Value, class Error>
1369 const Value* get_pointer(const Expected<Value, Error>& ex) noexcept {
1370 return ex.get_pointer();
1373 template <class Value, class Error>
1374 Value* get_pointer(Expected<Value, Error>& ex) noexcept {
1375 return ex.get_pointer();
1379 * For constructing an Expected object from a value, with the specified
1380 * Error type. Usage is as follows:
1382 * enum MyErrorCode { BAD_ERROR, WORSE_ERROR };
1383 * Expected<int, MyErrorCode> myAPI() {
1385 * return i ? makeExpected<MyErrorCode>(i) : makeUnexpected(BAD_ERROR);
1388 template <class Error, class Value>
1389 constexpr Expected<typename std::decay<Value>::type, Error> makeExpected(
1391 return Expected<typename std::decay<Value>::type, Error>{
1392 in_place, static_cast<Value&&>(val)};
1395 // Suppress comparability of Optional<T> with T, despite implicit conversion.
1396 template <class Value, class Error>
1397 bool operator==(const Expected<Value, Error>&, const Value& other) = delete;
1398 template <class Value, class Error>
1399 bool operator!=(const Expected<Value, Error>&, const Value& other) = delete;
1400 template <class Value, class Error>
1401 bool operator<(const Expected<Value, Error>&, const Value& other) = delete;
1402 template <class Value, class Error>
1403 bool operator<=(const Expected<Value, Error>&, const Value& other) = delete;
1404 template <class Value, class Error>
1405 bool operator>=(const Expected<Value, Error>&, const Value& other) = delete;
1406 template <class Value, class Error>
1407 bool operator>(const Expected<Value, Error>&, const Value& other) = delete;
1408 template <class Value, class Error>
1409 bool operator==(const Value& other, const Expected<Value, Error>&) = delete;
1410 template <class Value, class Error>
1411 bool operator!=(const Value& other, const Expected<Value, Error>&) = delete;
1412 template <class Value, class Error>
1413 bool operator<(const Value& other, const Expected<Value, Error>&) = delete;
1414 template <class Value, class Error>
1415 bool operator<=(const Value& other, const Expected<Value, Error>&) = delete;
1416 template <class Value, class Error>
1417 bool operator>=(const Value& other, const Expected<Value, Error>&) = delete;
1418 template <class Value, class Error>
1419 bool operator>(const Value& other, const Expected<Value, Error>&) = delete;
1421 } // namespace folly
1423 #if defined(__GNUC__) && !defined(__clang__)
1424 #pragma GCC diagnostic pop
1427 #undef FOLLY_REQUIRES
1428 #undef FOLLY_REQUIRES_TRAILING
1430 // Enable the use of folly::Expected with `co_await`
1431 // Inspired by https://github.com/toby-allsopp/coroutine_monad
1432 #if FOLLY_HAS_COROUTINES
1433 #include <experimental/coroutine>
1436 namespace expected_detail {
1437 template <typename Value, typename Error>
1440 template <typename Value, typename Error>
1441 struct PromiseReturn {
1442 Optional<Expected<Value, Error>> storage_;
1443 Promise<Value, Error>* promise_;
1444 /* implicit */ PromiseReturn(Promise<Value, Error>& promise) noexcept
1445 : promise_(&promise) {
1446 promise_->value_ = &storage_;
1448 PromiseReturn(PromiseReturn&& that) noexcept
1449 : PromiseReturn{*that.promise_} {}
1451 /* implicit */ operator Expected<Value, Error>() & {
1452 return std::move(*storage_);
1456 template <typename Value, typename Error>
1458 Optional<Expected<Value, Error>>* value_ = nullptr;
1459 Promise() = default;
1460 Promise(Promise const&) = delete;
1461 // This should work regardless of whether the compiler generates:
1462 // folly::Expected<Value, Error> retobj{ p.get_return_object(); } // MSVC
1464 // auto retobj = p.get_return_object(); // clang
1465 PromiseReturn<Value, Error> get_return_object() noexcept {
1468 std::experimental::suspend_never initial_suspend() const noexcept {
1471 std::experimental::suspend_never final_suspend() const {
1474 template <typename U>
1475 void return_value(U&& u) {
1476 value_->emplace(static_cast<U&&>(u));
1478 void unhandled_exception() {
1479 // Technically, throwing from unhandled_exception is underspecified:
1480 // https://github.com/GorNishanov/CoroutineWording/issues/17
1485 template <typename Value, typename Error>
1487 Expected<Value, Error> o_;
1489 explicit Awaitable(Expected<Value, Error> o) : o_(std::move(o)) {}
1491 bool await_ready() const noexcept {
1492 return o_.hasValue();
1494 Value await_resume() {
1495 return std::move(o_.value());
1498 // Explicitly only allow suspension into a Promise
1499 template <typename U>
1500 void await_suspend(std::experimental::coroutine_handle<Promise<U, Error>> h) {
1501 *h.promise().value_ = makeUnexpected(std::move(o_.error()));
1502 // Abort the rest of the coroutine. resume() is not going to be called
1506 } // namespace expected_detail
1508 template <typename Value, typename Error>
1509 expected_detail::Awaitable<Value, Error>
1510 /* implicit */ operator co_await(Expected<Value, Error> o) {
1511 return expected_detail::Awaitable<Value, Error>{std::move(o)};
1513 } // namespace folly
1515 // This makes folly::Optional<Value> useable as a coroutine return type..
1516 FOLLY_NAMESPACE_STD_BEGIN
1517 namespace experimental {
1518 template <typename Value, typename Error, typename... Args>
1519 struct coroutine_traits<folly::Expected<Value, Error>, Args...> {
1520 using promise_type = folly::expected_detail::Promise<Value, Error>;
1522 } // namespace experimental
1523 FOLLY_NAMESPACE_STD_END
1524 #endif // FOLLY_HAS_COROUTINES