Path: blob/main/contrib/llvm-project/libcxx/include/__expected/unexpected.h
35262 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8#ifndef _LIBCPP___EXPECTED_UNEXPECTED_H9#define _LIBCPP___EXPECTED_UNEXPECTED_H1011#include <__config>12#include <__type_traits/conjunction.h>13#include <__type_traits/is_array.h>14#include <__type_traits/is_const.h>15#include <__type_traits/is_constructible.h>16#include <__type_traits/is_nothrow_constructible.h>17#include <__type_traits/is_object.h>18#include <__type_traits/is_same.h>19#include <__type_traits/is_swappable.h>20#include <__type_traits/is_volatile.h>21#include <__type_traits/negation.h>22#include <__type_traits/remove_cvref.h>23#include <__utility/forward.h>24#include <__utility/in_place.h>25#include <__utility/move.h>26#include <__utility/swap.h>27#include <initializer_list>2829#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)30# pragma GCC system_header31#endif3233_LIBCPP_PUSH_MACROS34#include <__undef_macros>3536#if _LIBCPP_STD_VER >= 233738_LIBCPP_BEGIN_NAMESPACE_STD3940template <class _Err>41class unexpected;4243template <class _Tp>44struct __is_std_unexpected : false_type {};4546template <class _Err>47struct __is_std_unexpected<unexpected<_Err>> : true_type {};4849template <class _Tp>50using __valid_std_unexpected = _BoolConstant< //51is_object_v<_Tp> && //52!is_array_v<_Tp> && //53!__is_std_unexpected<_Tp>::value && //54!is_const_v<_Tp> && //55!is_volatile_v<_Tp> //56>;5758template <class _Err>59class unexpected {60static_assert(__valid_std_unexpected<_Err>::value,61"[expected.un.general] states a program that instantiates std::unexpected for a non-object type, an "62"array type, a specialization of unexpected, or a cv-qualified type is ill-formed.");6364public:65_LIBCPP_HIDE_FROM_ABI constexpr unexpected(const unexpected&) = default;66_LIBCPP_HIDE_FROM_ABI constexpr unexpected(unexpected&&) = default;6768template <class _Error = _Err>69requires(!is_same_v<remove_cvref_t<_Error>, unexpected> && //70!is_same_v<remove_cvref_t<_Error>, in_place_t> && //71is_constructible_v<_Err, _Error>)72_LIBCPP_HIDE_FROM_ABI constexpr explicit unexpected(_Error&& __error) //73noexcept(is_nothrow_constructible_v<_Err, _Error>) // strengthened74: __unex_(std::forward<_Error>(__error)) {}7576template <class... _Args>77requires is_constructible_v<_Err, _Args...>78_LIBCPP_HIDE_FROM_ABI constexpr explicit unexpected(in_place_t, _Args&&... __args) //79noexcept(is_nothrow_constructible_v<_Err, _Args...>) // strengthened80: __unex_(std::forward<_Args>(__args)...) {}8182template <class _Up, class... _Args>83requires is_constructible_v<_Err, initializer_list<_Up>&, _Args...>84_LIBCPP_HIDE_FROM_ABI constexpr explicit unexpected(in_place_t, initializer_list<_Up> __il, _Args&&... __args) //85noexcept(is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>) // strengthened86: __unex_(__il, std::forward<_Args>(__args)...) {}8788_LIBCPP_HIDE_FROM_ABI constexpr unexpected& operator=(const unexpected&) = default;89_LIBCPP_HIDE_FROM_ABI constexpr unexpected& operator=(unexpected&&) = default;9091_LIBCPP_HIDE_FROM_ABI constexpr const _Err& error() const& noexcept { return __unex_; }92_LIBCPP_HIDE_FROM_ABI constexpr _Err& error() & noexcept { return __unex_; }93_LIBCPP_HIDE_FROM_ABI constexpr const _Err&& error() const&& noexcept { return std::move(__unex_); }94_LIBCPP_HIDE_FROM_ABI constexpr _Err&& error() && noexcept { return std::move(__unex_); }9596_LIBCPP_HIDE_FROM_ABI constexpr void swap(unexpected& __other) noexcept(is_nothrow_swappable_v<_Err>) {97static_assert(is_swappable_v<_Err>, "unexpected::swap requires is_swappable_v<E> to be true");98using std::swap;99swap(__unex_, __other.__unex_);100}101102_LIBCPP_HIDE_FROM_ABI friend constexpr void swap(unexpected& __x, unexpected& __y) noexcept(noexcept(__x.swap(__y)))103requires is_swappable_v<_Err>104{105__x.swap(__y);106}107108template <class _Err2>109_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const unexpected& __x, const unexpected<_Err2>& __y) {110return __x.__unex_ == __y.__unex_;111}112113private:114_Err __unex_;115};116117template <class _Err>118unexpected(_Err) -> unexpected<_Err>;119120_LIBCPP_END_NAMESPACE_STD121122#endif // _LIBCPP_STD_VER >= 23123124_LIBCPP_POP_MACROS125126#endif // _LIBCPP___EXPECTED_UNEXPECTED_H127128129