std::future<T>::valid
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Concurrency support library
(C++11)
(C++20)
(C++11)
(C++11)
(C++20)
(C++26)
(C++26)
(C++20)
(C++26)
(C++20)
(C++26)
(C++26)
(C++26)
(C++26)
(C++26)
(C++26)
(C++11)
(C++11)
(C++17)
(C++11)
(C++14)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++20)(C++20)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++26)
(C++26)
(C++26)
(C++11)
(C++20)
(C++11)
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
(C++11)
(C++11)
(C++11)(deprecated in C++26)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++26)(C++26)
(C++26)(C++26)
(C++11)
(C++20)(C++20)
(C++20)
(C++20)
(C++11)(C++11)
(C++11)(C++11)
(C++20)(C++20)
(C++20)(C++20)
(C++20)
(C++20)
std::future
Member functions
Getting the result
State
future::valid
bool valid() const noexcept;
(since C++11)
Checks if the future refers to a shared state.
This is the case only for futures that were not default-constructed or moved from (i.e. returned by std::promise::get_future() , std::packaged_task::get_future() or std::async() ) until the first time get() or share() is called.
The behavior is undefined if any member function other than the destructor, the move-assignment operator, or valid
is called on a future
that does not refer to shared state (although implementations are encouraged to throw std::future_error indicating no_state
in this case). It is valid to move from a future object for which valid()
is false.
Contents
[edit] Parameters
(none)
[edit] Return value
true if *this refers to a shared state, otherwise false.
[edit] Example
Run this code
#include <future> #include <iostream> int main() { std::promise <void> p; std::future <void> f = p.get_future(); std::cout << std::boolalpha ; std::cout << f.valid() << '\n'; p.set_value(); std::cout << f.valid() << '\n'; f.get(); std::cout << f.valid() << '\n'; }
Output:
true true false