std::unique...operatorbool(3)

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

πŸ’– NAME πŸ’–

okay bestie, this is the NAME section and it's giving main character energy: std::unique_ptr::operatorbool β€” yes, std::unique_ptr::operatorbool. That's the name, twice, because the manual said so and we respect the manual. 🫢

This page is std::unique...operatorbool(3) from the C++ Standard Libary manual. (Yes, that's the spelling in the header β€” we're keeping it exactly, no edits, no cap. πŸ’…)

✨ Synopsis ✨

explicit operator bool() const noexcept;  (since C++11)
                                          (constexpr since C++23)

So it's explicit, it's noexcept, it's been here since C++11, and it's constexpr since C++23. glow-up confirmed ✨

It checks whether *this owns an object, i.e. whether get() != nullptr. That's the whole vibe check. πŸ‘‘

🫢 Parameters 🫢

(none) β€” literally no parameters. She takes nothing. Zero drama. πŸŽ€

⭐ Return value ⭐

true if *this owns an object, false otherwise. simple, clean, iconic. Periodt. πŸ’…

πŸŽ€ Example πŸŽ€

The page says // Run this code, so here's the exact code:

#include <iostream>
#include <memory>

int main()
{
    std::unique_ptr<int> ptr(new int(42));

    if (ptr)
        std::cout << "before reset, ptr is: " << *ptr << '\n';
    ptr.reset();
    ptr ? (std::cout << "after reset, ptr is: " << *ptr)
        : (std::cout << "after reset ptr is empty") << '\n';
}

🌈 Output: 🌈

before reset, ptr is: 42
after reset ptr is empty

πŸ” See also πŸ”

  • get β€” returns a pointer to the managed object (public member function). iykyk. πŸ¦‹

That's the whole page, bestie 🫢 source: http://cppreference.com, version/date: 2024.06.10, manual page: std::unique...operatorbool(3). Now go forth and check those pointers πŸ’–βœ¨

πŸ“¦ where this page comes from