Stay organized with collections Save and categorize content based on your preferences.
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:
StatusOr<Foo> foo = FetchFoo();
if (!foo) { // Same as !foo.ok()
// handle error and probably look at foo.status()
} else {
foo->DoSomethingFooey(); // UB if !foo
}
Alternatively, you may call the StatusOr::value()
member function, which is defined to: (1) throw an exception if there is no T
value, or (2) crash the program if exceptions are disabled. It is never UB to call value()
.
StatusOr<Foo> foo = FetchFoo();
foo.value().DoSomethingFooey(); // May throw/crash if there is no 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
<T>
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:
class Bar {
public:
Bar(Arg arg);
...
};
StatusOr<Bar> MakeBar() {
... complicated logic that might fail
return Bar(std::move(arg));
}
StatusOr
<T>
supports equality comparisons if the underlying type T
does.
Initializes with an error status (StatusCode::kUnknown
).
StatusOr const &
other
StatusOr &&
Creates a new StatusOr
<T>
holding the error condition rhs
.
rhs
Status
the status to initialize the object.
Exceptions Type Descriptionstd::invalid_argument
if `rhs.ok()`. If exceptions are disabled the program terminates via [`google::cloud::Terminate()`](xref:group__terminate_1gac5d2d48b27f2cd1de9075a1c9522f0cf) StatusOr(T &&)
Creates a new StatusOr
<T>
holding the value rhs
.
rhs
T &&
the value used to initialize the object.
Exceptions Type Description...
If `T`'s move constructor throws. StatusOr(T const &)
Creates a new StatusOr
<T>
holding the value rhs
.
rhs
T const &
the value used to initialize the object.
Exceptions Type Description...
If `T` copy constructor throws. Operators operator*() &
Dereference operators.
Returns Type DescriptionT &
operator*() const &
Dereference operators.
Returns Type DescriptionT const &
operator*() &&
Dereference operators.
Returns Type DescriptionT &&
operator*() const &&
Dereference operators.
Returns Type DescriptionT const &&
operator->() &
Member access operators.
Returns Type DescriptionT *
operator->() const &
Member access operators.
Returns Type DescriptionT const *
operator=(StatusOr const &) Parameter Name Description
StatusOr const &
StatusOr &
operator=(StatusOr &&) Parameter Name Description other
StatusOr &&
StatusOr &
operator=(Status)
Assigns the given non-OK Status to this StatusOr
<T>
.
status
Status
std::invalid_argument
if `status.ok()`. If exceptions are disabled the program terminates via [`google::cloud::Terminate()`](xref:group__terminate_1gac5d2d48b27f2cd1de9075a1c9522f0cf) Returns Type Description StatusOr &
operator=(U &&)
Assign a T
(or anything convertible to T
) into the StatusOr
.
This function does not participate in overload resolution if U
is equal to StatusOr
<T>
(or to a cv-ref-qualified StatusOr
<T>
).
rhs
U &&
typename U
a type convertible to T
.
StatusOr &
a reference to this object.
operator bool() constReturns true
when this
holds a value.
Value accessors.
Returns Type DescriptionT &
value() const &
Value accessors.
Returns Type DescriptionT const &
value() &&
Value accessors.
Returns Type DescriptionT &&
value() const &&
Value accessors.
Returns Type DescriptionT const &&
status() const &
Status accessors.
Returns Type DescriptionStatus const &
status() &&
Status accessors.
Returns Type DescriptionStatus &&
ok() const
Returns true
when this
holds a value.
bool
Type Aliases value_type
Alias Of: T
A value_type
member for use in generic programming.
This is analogous to that of std::optional::value_type
.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-14 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-14 UTC."],[[["The page documents various versions of `StatusOr`, a class representing either a value or a `Status` indicating the absence of a value."],["`StatusOr\u003cT\u003e` functions similarly to a smart pointer, requiring validation before dereferencing to access the contained value."],["`StatusOr` can be used by functions to return either a value or an error `Status`, providing callers the flexibility to handle errors as needed."],["Constructors of `StatusOr` handle value or error status initialization, and offer different methods to construct one using `Status`, `T const &`, or `T &&`."],["`StatusOr` provides various operators for dereferencing, member access, and assignment and member functions to get the value or the status."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4