llvm::ErrorOr

Represents either an error or a value T.

Synopsis

Declared in <llvm/Support/ErrorOr.h>

template<class T>
class ErrorOr;

Description

ErrorOr<T> is a pointer-like class that represents the result of an operation. The result is either an error, or a value of type T. This is designed to emulate the usage of returning a pointer where nullptr indicates failure. However instead of just knowing that the operation failed, we also have an error_code and optional user data that describes why it failed.

It is used like the following.


ErrorOr<Buffer> getBuffer();

auto buffer = getBuffer();
if (error_code ec = buffer.getError())
  return ec;
buffer->write("adena");

Implicit conversion to bool returns true if there is a usable value. The unary * and -> operators provide pointer like access to the value. Accessing the value when there is an error has undefined behavior.

When T is a reference type the behavior is slightly different. The reference is held in a std::reference_wrapper<std::remove_reference<T>::type>, and there is special handling to make operator -> work as if T was not a reference.

T cannot be a rvalue reference.

Type Aliases

NameDescription
storage_type Storage type for either a value or an error code.

Member Functions

NameDescription
ErrorOr [constructor]Constructors
~ErrorOr [destructor]Destroy the contained value or error code.
operator= Assignment operators
get get overloads
getError Return the stored error code, or a default-constructed code on success.
operator* Dereference operators
operator-> Member access operators
operator bool Return false if there is an error.

Data Members

NameDescription
ErrorStorage [variant member]Storage for the error code on failure.
TStorage [variant member]Storage for the successful value.

Friends

NameDescription
llvm::ErrorOrRepresents either an error or a value T.

Non-Member Functions

NameDescription
errorOrToExpectedConvert an ErrorOr<T> to an Expected<T>.
expectedToErrorOrConvert an Expected<T> to an ErrorOr<T>.
expectedToErrorOrAndEmitErrorsConvert Val to ErrorOr, emitting diagnostics into Ctx on failure.
needConversionQuery whether a file needs conversion to the UTF-8 codepage.
operator==Return true if Err holds error-code enum Code.
sys::findProgramByNameFind the first executable file Name in Paths.
sys::fs::disk_spaceGet disk space usage information.
sys::fs::getPermissionsGet file permissions.
sys::fs::md5_contentsCompute an MD5 hash of the file at Path.
sys::fs::md5_contentsCompute an MD5 hash of a file's contents.