std::shared_future::valid(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME πŸ’…

Okay so bestie, welcome to std::shared_future::valid(3), straight from the C++ Standard Libary manual β€” yes, that's the spelling in the header, and we are respecting the typo 😭. The NAME is literally std::shared_future::valid - std::shared_future::valid; it's giving "I contain multitudes" energy πŸ’–.


✨ Synopsis ✨

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 β€” no cap.

Unlike std::future, std::shared_future's shared state is not invalidated when get() is called. The way that survives get() is lowkey iconic πŸ’….

🚨 Serious bestie warning: the behavior is undefined if any member function other than the destructor, the copy-assignment operator, the move-assignment operator, or valid is called on a shared_future that does not refer to shared state. Implementations are encouraged to throw std::future_error indicating no_state in this case β€” but that's encouragement, not a guarantee. Do not test this. It is not the vibe. πŸ’€

It is valid to move or copy from a shared_future object for which valid() is false β€” that part is safe and slay πŸŽ€.


πŸŽ€ Parameters πŸŽ€

(none) β€” literally no parameters. Zero. Zilch. Not even a little guy πŸ™ˆ.


🌸 Return value 🌸

true if *this refers to a shared state, otherwise false. That's it, that's the return value, and it understood the assignment ✨.


πŸ¦‹ Example πŸ¦‹

It says // Run this code, then it does this:

#include <future>
#include <iostream>

int main()
{
    std::promise<void> p;
    std::shared_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
true

πŸ‘‘ See also πŸ‘‘

  • wait β€” waits for the result to become available (public member function). That's the only see-also, and it's carrying the whole reference section on its back πŸ’ͺ.

That's the whole page, bestie. It came from http://cppreference.com, dated 2024.06.10, and it's std::shared_future::valid(3) β€” bye, love u, mwah πŸ’‹πŸ’–

πŸ“¦ where this page comes from