A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/future::valid below:

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 valuetrue 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 safetyNo-throw guarantee: never throws exceptions.

See also
future::get
Get value (public member function)
future::wait
Wait for ready (public member function)

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