LLVM: llvm::ErrorOr< T > Class Template Reference (original) (raw)
template<class T>
class llvm::ErrorOr< T >
Represents either an error or a value T.
ErrorOr 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.
auto buffer = getBuffer();
if (error_code ec = buffer.getError())
return ec;
buffer->write("adena");
Represents either an error or a value T.
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::type>, and there is special handling to make operator -> work as if T was not a reference.
T cannot be a rvalue reference.