public member function
<future>

std::future::valid

bool valid() const noexcept;
Check for valid shared state
Returns whether the future object is currently associated with a shared state.

For default-constructed future objects, this function returns false (unless move-assigned a valid future).

Futures can only be initially constructed with valid shared states by certain provider functions, such as async, promise::get_future or packaged_task::get_future.

Once the value of the shared state is retrieved with future::get, calling this function returns false (unless move-assigned a new valid future).

Parameters

none

Return value

true if the object is associated with a shared state.
false otherwise.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// future::valid
#include <iostream>       // std::cout
#include <future>         // std::async, std::future
#include <utility>        // std::move

int get_value() { return 10; }

int main ()
{
  std::future<int> foo,bar;
  foo = std::async (get_value);
  bar = std::move(foo);

  if (foo.valid())
    std::cout << "foo's value: " << foo.get() << '\n';
  else
    std::cout << "foo is not valid\n";

  if (bar.valid())
    std::cout << "bar's value: " << bar.get() << '\n';
  else
    std::cout << "bar is not valid\n";

  return 0;
}


Output:

foo is not valid
bar's value: 10

Data races

The future object is accessed.

Exception safety

No-throw guarantee: never throws exceptions.

See also