std::unique_ptr::get(3)

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

okay bestie, welcome to std::unique_ptr::get(3) β€” the C++ Standard Libary manual page for std::unique_ptr::get πŸ’…βœ¨ this is the page where C++ says: here's how you ask your std::unique_ptr for its pointer. no drama, no cap.

🌸 NAME 🌸

std::unique_ptr::get - std::unique_ptr::get

okay so the NAME line is literally the same name twice β€” minimal, iconic, ate πŸ’…. that's it. that's the NAME.

✨ Synopsis ✨

vibe check, here's the signature exactly as the page has it:

          pointer get() const noexcept;  (since C++11)
                                         (constexpr since C++23)

and the description lowkey says it all: it returns a pointer to the managed object, or nullptr if no object is owned ✨.

πŸŽ€ Parameters πŸŽ€

(none) β€” literally no parameters, bestie, not a single one πŸ™ˆ.

πŸ’– Return value πŸ’–

Pointer to the managed object, or nullptr if no object is owned. that's the whole return-value story, periodt πŸ’– fr fr.

πŸ¦‹ Example πŸ¦‹

bestie, this example is a whole main-character moment: a Res gets made, up.get() hands over a Res*, we print it, and then the destructor does its thing. let's go πŸ¦‹:

       // Run this code

        #include <iomanip>
        #include <iostream>
        #include <memory>
        #include <string>
        #include <utility>

        class Res
        {
            std::string s;

        public:
            Res(std::string arg) : s{std::move(arg)}
            {
                std::cout << "Res::Res(" << std::quoted(s) << ");\n";
            }

            ~Res()
            {
                std::cout << "Res::~Res();\n";
            }

        private:
            friend std::ostream& operator<<(std::ostream& os, Res const& r)
            {
                return os << "Res { s = " << std::quoted(r.s) << "; }";
            }
        };

        int main()
        {
            std::unique_ptr<Res> up(new Res{"Hello, world!"});
            Res* res = up.get();
            std::cout << *res << '\n';
        }

🌈 Output: 🌈

and it prints, literally:

        Res::Res("Hello, world!");
        Res { s = "Hello, world!"; }
        Res::~Res();

πŸ‘‘ See also πŸ‘‘

  • release β€” returns a pointer to the managed object and releases the ownership (public member function). it's giving "same pointer, but ownership leaves the chat" πŸ‘‘.

source/version: http://cppreference.com; date: 2024.06.10. that's the std::unique_ptr::get(3) page, bestie β€” now go get that pointer πŸ’…βœ¨

πŸ“¦ where this page comes from