3.11.0-rc 3.10.0 (latest) 3.9.0 3.8.0 3.7.0 3.6.0 3.5.0 3.4.0 3.3.0 3.2.0 2.48.0-rc 2.47.1 2.46.0 2.45.0 2.44.0 2.43.0 2.42.0 2.41.0 2.40.0 2.39.0 2.38.0 2.37.0 2.36.0 2.35.0 2.34.0 2.33.0 2.32.0 2.31.0 2.30.0 2.29.0 2.28.0 2.27.0 2.26.0 2.25.1 2.24.0 2.23.0 2.22.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.14.0 2.13.0 2.12.0 2.11.0 2.10.1 Holds a value or a [`Status`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1Status) indicating why there is no value.
[`StatusOr`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr)`<T>` represents either a usable `T` value or a [`Status`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1Status) object explaining why a `T` value is not present. Typical usage of [`StatusOr`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr)`<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()`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1afce69df5d31d0fba3bd95b317580ae11)), then you may dereference the object to access the contained value.

It is undefined behavior (UB) to dereference a [`StatusOr`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr)`<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()`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/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()`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1aeb1f35f6c7b8b5d56641a6f2bb1c9697).

    StatusOr<Foo> foo = FetchFoo();
    foo.value().DoSomethingFooey();  // May throw/crash if there is no value

Functions that can fail will often return a [`StatusOr`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr)`<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`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr)`<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`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr)`<T>`. For example:

    class Bar {
     public:
      Bar(Arg arg);
      ...
    };
    StatusOr<Bar> MakeBar() {
      ... complicated logic that might fail
      return Bar(std::move(arg));
    }

[`StatusOr`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr)`<T>` supports equality comparisons if the underlying type `T` does.

## Constructors

### StatusOr()

Initializes with an error status ([`StatusCode::kUnknown`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/namespacegoogle_1_1cloud_1a90e17f75452470f0f3ee1a06ffe58847#namespacegoogle_1_1cloud_1a90e17f75452470f0f3ee1a06ffe58847a25c2dc47991b3df171ed5192bcf70390)).

### StatusOr(StatusOr const \&)

| **Parameter** ||
|---|---|
| **Name** | **Description** |
|   | `StatusOr const &` |

### StatusOr(StatusOr \&\&)

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `other` | `StatusOr &&` |

### StatusOr(Status)

Creates a new [`StatusOr`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr)`<T>` holding the error condition `rhs`.

###### Post-conditions

[`ok()`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1afce69df5d31d0fba3bd95b317580ae11)`== false` and [`status()`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1a46da72dc9d35a019bd1359f4b3b4f8bd)`== rhs`.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `rhs` | `Status` the status to initialize the object. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `std::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`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr)`<T>` holding the value `rhs`.

###### Post-conditions

[`ok()`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1afce69df5d31d0fba3bd95b317580ae11)`== true` and [`value()`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1aeb1f35f6c7b8b5d56641a6f2bb1c9697)`== rhs`.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `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`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr)`<T>` holding the value `rhs`.

###### Post-conditions

[`ok()`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1afce69df5d31d0fba3bd95b317580ae11)`== true` and [`value()`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr#classgoogle_1_1cloud_1_1StatusOr_1aeb1f35f6c7b8b5d56641a6f2bb1c9697)`== rhs`.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `rhs` | `T const &` the value used to initialize the object. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `...` | If \`T\` copy constructor throws. |

## Operators

### operator\*() \&

Dereference operators.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T &` |   |

### operator\*() const \&

Dereference operators.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T const &` |   |

### operator\*() \&\&

Dereference operators.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T &&` |   |

### operator\*() const \&\&

Dereference operators.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T const &&` |   |

### operator-\>() \&

Member access operators.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T *` |   |

### operator-\>() const \&

Member access operators.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T const *` |   |

### operator=(StatusOr const \&)

| **Parameter** ||
|---|---|
| **Name** | **Description** |
|   | `StatusOr const &` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `StatusOr &` |   |

### operator=(StatusOr \&\&)

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `other` | `StatusOr &&` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `StatusOr &` |   |

### operator=(Status)

Assigns the given non-OK [Status](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1Status) to this [`StatusOr`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr)`<T>`.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `status` | `Status` |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `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`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr).
This function does not participate in overload resolution if `U` is equal to [`StatusOr`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr)`<T>` (or to a cv-ref-qualified [`StatusOr`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr)`<T>`).

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `rhs` | `U &&` |
| `typename U` | a type convertible to `T`. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `StatusOr &` | a reference to this object. |

### operator bool() const

Returns `true` when `this` holds 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

Returns `true` when `this` holds a value.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `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`.