std::any::has_value(3)

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

✨ std::any::has_value(3) ✨

okay so bestie, we're in the C++ Standard Libary manual (yes, that spelling, we're keeping it πŸŽ€), and this page is all about std::any::has_value. It's giving tiny-but-mighty member function energy πŸ’…

🌸 NAME 🌸

std::any::has_value β€” std::any::has_value. That's the whole name, no notes, no plot twist πŸ’–

✨ Synopsis ✨

          bool has_value() const noexcept;  (since C++17)

Checks whether the object contains a value.

πŸŽ€ Parameters πŸŽ€

  • (none) β€” literally zero parameters, bestie. The parameter list is empty and it's thriving πŸ¦‹

πŸ¦‹ Return value πŸ¦‹

true if the instance contains a value, otherwise false. No mysterious third option, no cap πŸ’€

πŸ”₯ Example πŸ”₯

Run this code, it's right here and it's iconic:

       // Run this code

        #include <any>
        #include <iostream>
        #include <string>

        int main()
        {
            std::boolalpha(std::cout);

            std::any a0;
            std::cout << "a0.has_value(): " << a0.has_value() << '\n';

            std::any a1 = 42;
            std::cout << "a1.has_value(): " << a1.has_value() << '\n';
            std::cout << "a1 = " << std::any_cast<int>(a1) << '\n';
            a1.reset();
            std::cout << "a1.has_value(): " << a1.has_value() << '\n';

            auto a2 = std::make_any<std::string>("Milky Way");
            std::cout << "a2.has_value(): " << a2.has_value() << '\n';
            std::cout << "a2 = \"" << std::any_cast<std::string&>(a2) <<
       "\"\n";
            a2.reset();
            std::cout << "a2.has_value(): " << a2.has_value() << '\n';
        }

⭐ Output ⭐

and the output is serving:

        a0.has_value(): false
        a1.has_value(): true
        a1 = 42
        a1.has_value(): false
        a2.has_value(): true
        a2 = "Milky Way"
        a2.has_value(): false

πŸ’œ See also πŸ’œ

  • reset β€” destroys contained object (public member function) 🫢

okay that's the page! source/version info: http://cppreference.com, 2024.06.10, and the page is std::any::has_value(3) β€” mwah, bestie, go check your any πŸ’–βœ¨

πŸ“¦ where this page comes from