Holds a value or aStatusindicating why there is no value.
StatusOr<T>represents either a usableTvalue or aStatusobject explaining why aTvalue is not present. Typical usage ofStatusOr<T>looks like usage of a smart pointer, or even astd::optional<T>, in that you first check its validity using a conversion to bool (or by callingStatusOr::ok()), then you may dereference the object to access the contained value.
It is undefined behavior (UB) to dereference aStatusOr<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 theStatusOr::value()member function, which is defined to: (1) throw an exception if there is noTvalue, or (2) crash the program if exceptions are disabled. It is never UB to callvalue().
StatusOr<Foo> foo = FetchFoo();
foo.value().DoSomethingFooey(); // May throw/crash if there is no value
Functions that can fail will often return aStatusOr<T>instead of returning an error code and taking aTout-param, or rather than directly returning theTand 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 aStatusOr<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 typeTdoes.
if `rhs.ok()`. If exceptions are disabled the program terminates via [`google::cloud::Terminate()`](xref:group__terminate_1gac5d2d48b27f2cd1de9075a1c9522f0cf)
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 aT(or anything convertible toT) into theStatusOr.
This function does not participate in overload resolution ifUis equal toStatusOr<T>(or to a cv-ref-qualifiedStatusOr<T>).
Parameters
Name
Description
rhs
U &&
typename U
a type convertible toT.
Returns
Type
Description
StatusOr &
a reference to this object.
operator bool() const
Returnstruewhenthisholds a value.
Functions
value() &
Value accessors.
Returns
Type
Description
T &
value() const &
Value accessors.
Returns
Type
Description
T const &
value() &&
Value accessors.
Returns
Type
Description
T &&
value() const &&
Value accessors.
Returns
Type
Description
T const &&
status() const &
Status accessors.
Returns
Type
Description
Status const &
status() &&
Status accessors.
Returns
Type
Description
Status &&
ok() const
Returnstruewhenthisholds a value.
Returns
Type
Description
bool
Type Aliases
value_type
Alias Of:T
Avalue_typemember for use in generic programming.
This is analogous to that ofstd::optional::value_type.
[[["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-09-04 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eStatusOr<T>\u003c/code\u003e class represents a container that either holds a value of type \u003ccode\u003eT\u003c/code\u003e or a \u003ccode\u003eStatus\u003c/code\u003e object, indicating the absence of a valid value.\u003c/p\u003e\n"],["\u003cp\u003eUsers can check the validity of a \u003ccode\u003eStatusOr<T>\u003c/code\u003e object using a boolean conversion or the \u003ccode\u003eok()\u003c/code\u003e method, and dereferencing a non-valid object leads to undefined behavior.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003evalue()\u003c/code\u003e method allows direct access to the contained value but may throw an exception or crash the program if the object does not hold a value.\u003c/p\u003e\n"],["\u003cp\u003eConstructors for \u003ccode\u003eStatusOr<T>\u003c/code\u003e support initializing with a \u003ccode\u003eStatus\u003c/code\u003e, \u003ccode\u003eT\u003c/code\u003e, or another \u003ccode\u003eStatusOr<T>\u003c/code\u003e object, and it supports assignment operations with the same type parameters.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eStatusOr<T>\u003c/code\u003e class is used to return from functions that may fail, allowing the caller to either explicitly handle the error or to utilize functions such as value that are guaranteed not to be undefined behavior.\u003c/p\u003e\n"]]],[],null,["# Class StatusOr<T> (2.24.0)\n\nVersion 2.24.0keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr)\n- [2.41.0](/cpp/docs/reference/common/2.41.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.40.0](/cpp/docs/reference/common/2.40.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.39.0](/cpp/docs/reference/common/2.39.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.38.0](/cpp/docs/reference/common/2.38.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.37.0](/cpp/docs/reference/common/2.37.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.36.0](/cpp/docs/reference/common/2.36.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.35.0](/cpp/docs/reference/common/2.35.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.34.0](/cpp/docs/reference/common/2.34.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.33.0](/cpp/docs/reference/common/2.33.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.32.0](/cpp/docs/reference/common/2.32.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.31.0](/cpp/docs/reference/common/2.31.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.30.0](/cpp/docs/reference/common/2.30.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.29.0](/cpp/docs/reference/common/2.29.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.28.0](/cpp/docs/reference/common/2.28.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.27.0](/cpp/docs/reference/common/2.27.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.26.0](/cpp/docs/reference/common/2.26.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.25.1](/cpp/docs/reference/common/2.25.1/classgoogle_1_1cloud_1_1StatusOr)\n- [2.24.0](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.23.0](/cpp/docs/reference/common/2.23.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.22.1](/cpp/docs/reference/common/2.22.1/classgoogle_1_1cloud_1_1StatusOr)\n- [2.21.0](/cpp/docs/reference/common/2.21.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.20.0](/cpp/docs/reference/common/2.20.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.19.0](/cpp/docs/reference/common/2.19.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.18.0](/cpp/docs/reference/common/2.18.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.17.0](/cpp/docs/reference/common/2.17.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.16.0](/cpp/docs/reference/common/2.16.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.15.1](/cpp/docs/reference/common/2.15.1/classgoogle_1_1cloud_1_1StatusOr)\n- [2.14.0](/cpp/docs/reference/common/2.14.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.13.0](/cpp/docs/reference/common/2.13.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.12.0](/cpp/docs/reference/common/2.12.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.11.0](/cpp/docs/reference/common/2.11.0/classgoogle_1_1cloud_1_1StatusOr)\n- [2.10.1](/cpp/docs/reference/common/2.10.1/classgoogle_1_1cloud_1_1StatusOr) \nHolds a value or a [`Status`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1Status) indicating why there is no value. \n[`StatusOr`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr)`\u003cT\u003e` represents either a usable `T` value or a [`Status`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1Status) object explaining why a `T` value is not present. Typical usage of [`StatusOr`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr)`\u003cT\u003e` looks like usage of a smart pointer, or even a `std::optional\u003cT\u003e`, in that you first check its validity using a conversion to bool (or by calling [`StatusOr::ok()`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1afce69df5d31d0fba3bd95b317580ae11)), then you may dereference the object to access the contained value.\n\nIt is undefined behavior (UB) to dereference a [`StatusOr`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr)`\u003cT\u003e` that is not \"ok\". For example: \n\n StatusOr\u003cFoo\u003e foo = FetchFoo();\n if (!foo) { // Same as !foo.ok()\n // handle error and probably look at foo.status()\n } else {\n foo-\u003eDoSomethingFooey(); // UB if !foo\n }\n\nAlternatively, you may call the [`StatusOr::value()`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1aeb1f35f6c7b8b5d56641a6f2bb1c9697) 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()`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1aeb1f35f6c7b8b5d56641a6f2bb1c9697). \n\n StatusOr\u003cFoo\u003e foo = FetchFoo();\n foo.value().DoSomethingFooey(); // May throw/crash if there is no value\n\nFunctions that can fail will often return a [`StatusOr`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr)`\u003cT\u003e` 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`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr)`\u003cT\u003e` is used so that callers can choose whether they want to explicitly check for errors, crash the program, or throw exceptions.\n\nSince 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`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr)`\u003cT\u003e`. For example: \n\n class Bar {\n public:\n Bar(Arg arg);\n ...\n };\n StatusOr\u003cBar\u003e MakeBar() {\n ... complicated logic that might fail\n return Bar(std::move(arg));\n }\n\n[`StatusOr`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr)`\u003cT\u003e` supports equality comparisons if the underlying type `T` does.\n\nConstructors\n------------\n\n### StatusOr()\n\nInitializes with an error status ([`StatusCode::kUnknown`](/cpp/docs/reference/common/2.24.0/namespacegoogle_1_1cloud_1a90e17f75452470f0f3ee1a06ffe58847#namespacegoogle_1_1cloud_1a90e17f75452470f0f3ee1a06ffe58847a25c2dc47991b3df171ed5192bcf70390)).\n\n### StatusOr(StatusOr const \\&)\n\n### StatusOr(StatusOr \\&\\&)\n\n### StatusOr(Status)\n\nCreates a new [`StatusOr`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr)`\u003cT\u003e` holding the error condition `rhs`. \n\n###### Post-conditions\n\n[`ok()`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1afce69df5d31d0fba3bd95b317580ae11)`== false` and [`status()`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1a46da72dc9d35a019bd1359f4b3b4f8bd)`== rhs`.\n\n### StatusOr(T \\&\\&)\n\nCreates a new [`StatusOr`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr)`\u003cT\u003e` holding the value `rhs`. \n\n###### Post-conditions\n\n[`ok()`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1afce69df5d31d0fba3bd95b317580ae11)`== true` and [`value()`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1aeb1f35f6c7b8b5d56641a6f2bb1c9697)`== rhs`.\n\n### StatusOr(T const \\&)\n\nCreates a new [`StatusOr`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr)`\u003cT\u003e` holding the value `rhs`. \n\n###### Post-conditions\n\n[`ok()`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1afce69df5d31d0fba3bd95b317580ae11)`== true` and [`value()`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1aeb1f35f6c7b8b5d56641a6f2bb1c9697)`== rhs`.\n\nOperators\n---------\n\n### operator\\*() \\&\n\nDereference operators.\n\n### operator\\*() const \\&\n\nDereference operators.\n\n### operator\\*() \\&\\&\n\nDereference operators.\n\n### operator\\*() const \\&\\&\n\nDereference operators.\n\n### operator-\\\u003e() \\&\n\nMember access operators.\n\n### operator-\\\u003e() const \\&\n\nMember access operators.\n\n### operator=(StatusOr const \\&)\n\n### operator=(StatusOr \\&\\&)\n\n### operator=(Status)\n\nAssigns the given non-OK [Status](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1Status) to this [`StatusOr`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr)`\u003cT\u003e`.\n\n### operator=(U \\&\\&)\n\nAssign a `T` (or anything convertible to `T`) into the [`StatusOr`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr). \nThis function does not participate in overload resolution if `U` is equal to [`StatusOr`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr)`\u003cT\u003e` (or to a cv-ref-qualified [`StatusOr`](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1StatusOr)`\u003cT\u003e`).\n\n### operator bool() const\n\nReturns `true` when `this` holds a value.\n\nFunctions\n---------\n\n### value() \\&\n\nValue accessors.\n\n### value() const \\&\n\nValue accessors.\n\n### value() \\&\\&\n\nValue accessors.\n\n### value() const \\&\\&\n\nValue accessors.\n\n### status() const \\&\n\nStatus accessors.\n\n### status() \\&\\&\n\nStatus accessors.\n\n### ok() const\n\nReturns `true` when `this` holds a value.\n\nType Aliases\n------------\n\n### value_type\n\n**Alias Of** : `T` \nA `value_type` member for use in generic programming. \nThis is analogous to that of `std::optional::value_type`."]]