1 //===-- A data structure for returning an error or a value ------*- C++ -*-===// 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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_LIBC_SRC___SUPPORT_ERROR_OR_H 10 #define LLVM_LIBC_SRC___SUPPORT_ERROR_OR_H 11 12 #include "src/__support/CPP/expected.h" 13 #include "src/__support/macros/config.h" 14 15 namespace LIBC_NAMESPACE_DECL { 16 17 template <class T> using ErrorOr = cpp::expected<T, int>; 18 19 using Error = cpp::unexpected<int>; 20 21 // template <typename T> struct ErrorOr { 22 // union { 23 // T value; 24 // int error; 25 // }; 26 // bool is_error; 27 28 // constexpr ErrorOr(T value) : value(value), is_error(false) {} 29 // constexpr ErrorOr(int error, bool is_error) 30 // : error(error), is_error(is_error) {} 31 32 // constexpr bool has_error() { return is_error; } 33 34 // constexpr operator bool() { return is_error; } 35 // constexpr operator T() { return value; } 36 // }; 37 38 } // namespace LIBC_NAMESPACE_DECL 39 40 #endif // LLVM_LIBC_SRC___SUPPORT_ERROR_OR_H 41