Google Cloud C++ Client 2.8.0
C++ Client Library for Google Cloud Platform
|
Holds a value or a Status
indicating why there is no value.
More...
#include <google/cloud/status_or.h>
Public Types | |
using | value_type = T |
A value_type member for use in generic programming. More... | |
Public Member Functions | |
StatusOr () | |
Initializes with an error status (UNKNOWN). More... | |
StatusOr (StatusOr const &)=default | |
StatusOr & | operator= (StatusOr const &)=default |
StatusOr (StatusOr &&other) | |
StatusOr & | operator= (StatusOr &&other) |
StatusOr (Status rhs) | |
Creates a new StatusOr<T> holding the error condition rhs . More... | |
StatusOr & | operator= (Status status) |
Assigns the given non-OK Status to this StatusOr<T> . More... | |
template<typename U = T> | |
std::enable_if<!std::is_same< StatusOr, typenamestd::decay< U >::type >::value, StatusOr >::type & | operator= (U &&rhs) |
Assign a T (or anything convertible to T ) into the StatusOr . More... | |
StatusOr (T &&rhs) | |
Creates a new StatusOr<T> holding the value rhs . More... | |
StatusOr (T const &rhs) | |
bool | ok () const |
operator bool () const | |
Deference operators. | |
T & | operator* () & |
T const & | operator* () const & |
T && | operator* () && |
T const && | operator* () const && |
Member access operators. | |
T * | operator-> () & |
T const * | operator-> () const & |
Value accessors. | |
T & | value () & |
T const & | value () const & |
T && | value () && |
T const && | value () const && |
Status accessors. | |
| |
Status const & | status () const & |
Status && | status () && |
Holds a value or a Status
indicating why there is no value.
StatusOr<T>
represents either a usable T
value or a Status
object explaining why a T
value is not present. Typical usage of StatusOr<T>
looks like usage of a smart pointer, or even a std::optional<T>, in that you first check its validity using a conversion to bool (or by calling StatusOr::ok()
), then you may dereference the object to access the contained value. It is undefined behavior (UB) to dereference a StatusOr<T>
that is not "ok". For example:
Alternatively, you may call the StatusOr::value()
member function, which is defined to throw an exception if there is no T
value, or crash the program if exceptions are disabled. It is never UB to call .value()
.
Functions that can fail will often return a StatusOr<T>
instead of returning an error code and taking a T
out-param, or rather than directly returning the T
and throwing an exception on error. StatusOr is used so that callers can choose whether they want to explicitly check for errors, crash the program, or throw exceptions. Since constructors do not have a return value, they should be designed in such a way that they cannot fail by moving the object's complex initialization logic into a separate factory function that itself can return a StatusOr<T>
. For example:
StatusOr<T>
supports equality comparisons if the underlying type T
does.
TODO(...) - the current implementation is fairly naive with respect to T
, it is unlikely to work correctly for reference types, types without default constructors, arrays.
T | the type of the value. |
using google::cloud::StatusOr< T >::value_type = T |
A value_type
member for use in generic programming.
This is analogous to that of std::optional::value_type
.
|
inline |
Initializes with an error status (UNKNOWN).
|
default |
|
inline |
|
inline |
Creates a new StatusOr<T>
holding the error condition rhs
.
rhs | the status to initialize the object. |
std::invalid_argument | if rhs.ok() . If exceptions are disabled the program terminates via google::cloud::Terminate() |
|
inline |
|
inline |
|
inline |
|
inlineexplicit |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Assigns the given non-OK Status to this StatusOr<T>
.
std::invalid_argument | if status.ok() . If exceptions are disabled the program terminates via google::cloud::Terminate() |
|
inline |
|
default |
|
inline |
Assign a T
(or anything convertible to T
) into the StatusOr
.
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |