LLVM 22.0.0git
llvm::ErrorOr< T > Class Template Reference

Represents either an error or a value T. More...

#include "llvm/Support/ErrorOr.h"

Inheritance diagram for llvm::ErrorOr< T >:
[legend]

Public Types

using storage_type = std::conditional_t<isRef, wrap, T>

Public Member Functions

template<class E>
 ErrorOr (E ErrorCode, std::enable_if_t< std::is_error_code_enum< E >::value||std::is_error_condition_enum< E >::value, void * >=nullptr)
 ErrorOr (std::error_code EC)
template<class OtherT>
 ErrorOr (OtherT &&Val, std::enable_if_t< std::is_convertible_v< OtherT, T > > *=nullptr)
 ErrorOr (const ErrorOr &Other)
template<class OtherT>
 ErrorOr (const ErrorOr< OtherT > &Other, std::enable_if_t< std::is_convertible_v< OtherT, T > > *=nullptr)
template<class OtherT>
 ErrorOr (const ErrorOr< OtherT > &Other, std::enable_if_t<!std::is_convertible_v< OtherT, const T & > > *=nullptr)
 ErrorOr (ErrorOr &&Other)
template<class OtherT>
 ErrorOr (ErrorOr< OtherT > &&Other, std::enable_if_t< std::is_convertible_v< OtherT, T > > *=nullptr)
template<class OtherT>
 ErrorOr (ErrorOr< OtherT > &&Other, std::enable_if_t<!std::is_convertible_v< OtherT, T > > *=nullptr)
ErrorOroperator= (const ErrorOr &Other)
ErrorOroperator= (ErrorOr &&Other)
 ~ErrorOr ()
 operator bool () const
 Return false if there is an error.
reference get ()
const_reference get () const
std::error_code getError () const
pointer operator-> ()
const_pointer operator-> () const
reference operator* ()
const_reference operator* () const

Friends

template<class OtherT>
class ErrorOr

Detailed Description

template<class T>
class llvm::ErrorOr< T >

Represents either an error or a value T.

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");
friend class ErrorOr
Definition ErrorOr.h:57

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.

Definition at line 56 of file ErrorOr.h.

Member Typedef Documentation

◆ storage_type

template<class T>
using llvm::ErrorOr< T >::storage_type = std::conditional_t<isRef, wrap, T>

Definition at line 64 of file ErrorOr.h.

Constructor & Destructor Documentation

◆ ErrorOr() [1/9]

template<class T>
template<class E>
llvm::ErrorOr< T >::ErrorOr ( E ErrorCode,
std::enable_if_t< std::is_error_code_enum< E >::value||std::is_error_condition_enum< E >::value, void * > = nullptr )
inline

Definition at line 74 of file ErrorOr.h.

References E(), and llvm::make_error_code().

◆ ErrorOr() [2/9]

template<class T>
llvm::ErrorOr< T >::ErrorOr ( std::error_code EC)
inline

Definition at line 82 of file ErrorOr.h.

◆ ErrorOr() [3/9]

template<class T>
template<class OtherT>
llvm::ErrorOr< T >::ErrorOr ( OtherT && Val,
std::enable_if_t< std::is_convertible_v< OtherT, T > > * = nullptr )
inline

Definition at line 87 of file ErrorOr.h.

◆ ErrorOr() [4/9]

template<class T>
llvm::ErrorOr< T >::ErrorOr ( const ErrorOr< T > & Other)
inline

Definition at line 93 of file ErrorOr.h.

References ErrorOr, and llvm::Other.

◆ ErrorOr() [5/9]

template<class T>
template<class OtherT>
llvm::ErrorOr< T >::ErrorOr ( const ErrorOr< OtherT > & Other,
std::enable_if_t< std::is_convertible_v< OtherT, T > > * = nullptr )
inline

Definition at line 98 of file ErrorOr.h.

References ErrorOr, and llvm::Other.

◆ ErrorOr() [6/9]

template<class T>
template<class OtherT>
llvm::ErrorOr< T >::ErrorOr ( const ErrorOr< OtherT > & Other,
std::enable_if_t<!std::is_convertible_v< OtherT, const T & > > * = nullptr )
inlineexplicit

Definition at line 104 of file ErrorOr.h.

References ErrorOr, and llvm::Other.

◆ ErrorOr() [7/9]

template<class T>
llvm::ErrorOr< T >::ErrorOr ( ErrorOr< T > && Other)
inline

Definition at line 110 of file ErrorOr.h.

References ErrorOr, and llvm::Other.

◆ ErrorOr() [8/9]

template<class T>
template<class OtherT>
llvm::ErrorOr< T >::ErrorOr ( ErrorOr< OtherT > && Other,
std::enable_if_t< std::is_convertible_v< OtherT, T > > * = nullptr )
inline

Definition at line 115 of file ErrorOr.h.

References ErrorOr, and llvm::Other.

◆ ErrorOr() [9/9]

template<class T>
template<class OtherT>
llvm::ErrorOr< T >::ErrorOr ( ErrorOr< OtherT > && Other,
std::enable_if_t<!std::is_convertible_v< OtherT, T > > * = nullptr )
inlineexplicit

Definition at line 123 of file ErrorOr.h.

References ErrorOr, and llvm::Other.

◆ ~ErrorOr()

template<class T>
llvm::ErrorOr< T >::~ErrorOr ( )
inline

Definition at line 139 of file ErrorOr.h.

Member Function Documentation

◆ get() [1/2]

◆ get() [2/2]

template<class T>
const_reference llvm::ErrorOr< T >::get ( ) const
inline

Definition at line 150 of file ErrorOr.h.

References ErrorOr, and get().

Referenced by get().

◆ getError()

◆ operator bool()

template<class T>
llvm::ErrorOr< T >::operator bool ( ) const
inlineexplicit

Return false if there is an error.

Definition at line 145 of file ErrorOr.h.

◆ operator*() [1/2]

template<class T>
reference llvm::ErrorOr< T >::operator* ( )
inline

Definition at line 162 of file ErrorOr.h.

◆ operator*() [2/2]

template<class T>
const_reference llvm::ErrorOr< T >::operator* ( ) const
inline

Definition at line 166 of file ErrorOr.h.

◆ operator->() [1/2]

template<class T>
pointer llvm::ErrorOr< T >::operator-> ( )
inline

Definition at line 156 of file ErrorOr.h.

◆ operator->() [2/2]

template<class T>
const_pointer llvm::ErrorOr< T >::operator-> ( ) const
inline

Definition at line 160 of file ErrorOr.h.

◆ operator=() [1/2]

template<class T>
ErrorOr & llvm::ErrorOr< T >::operator= ( const ErrorOr< T > & Other)
inline

Definition at line 129 of file ErrorOr.h.

References ErrorOr, and llvm::Other.

◆ operator=() [2/2]

template<class T>
ErrorOr & llvm::ErrorOr< T >::operator= ( ErrorOr< T > && Other)
inline

Definition at line 134 of file ErrorOr.h.

References ErrorOr, and llvm::Other.

◆ ErrorOr

template<class T>
template<class OtherT>
friend class ErrorOr
friend

Definition at line 57 of file ErrorOr.h.

References ErrorOr.

Referenced by ErrorOr, ErrorOr(), ErrorOr(), ErrorOr(), ErrorOr(), ErrorOr(), ErrorOr(), get(), operator=(), and operator=().

Member Data Documentation

◆ ErrorStorage

template<class T>
std::error_code llvm::ErrorOr< T >::ErrorStorage

Definition at line 257 of file ErrorOr.h.

◆ TStorage

template<class T>
storage_type llvm::ErrorOr< T >::TStorage

Definition at line 256 of file ErrorOr.h.


The documentation for this class was generated from the following file: