std::unique_ptr::get(3)
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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms