std::unique...:operator->(3)

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

πŸŽ€ NAME πŸŽ€

Okay bestie, welcome to the C++ Standard Libary manual page for std::unique_ptr::operator*,std::unique_ptr::operator-> - std::unique_ptr::operator*,std::unique_ptr::operator->. Yep, the NAME line does the little repeated-name thing, and the page header vibe is std::unique...:operator->(3). We're folding the manual name in up here instead of copying that header line, periodt πŸ’….

πŸ¦‹ Synopsis πŸ¦‹

Here's the signature glow-up, keep it exact:

typename std::add_lvalue_reference<T>::type operator*() const
    noexcept(noexcept(*std::declval<pointer>()));  (1)  (since C++11)
                                                     (constexpr since C++23)

pointer operator->() const noexcept;  (2)  (since C++11)
                                           (constexpr since C++23)

operator* and operator-> provide access to the object owned by *this.

⚠️ Undefined behavior alert: the behavior is undefined if get() == nullptr. Do not do the thing, bestie. 😭

These member functions are only provided for unique_ptr for the single objects, i.e. the primary template.

🌸 Parameters 🌸

None. Nada. Zero. 🫢

✨ Return value ✨

  1. Returns the object owned by *this, equivalent to *get(). πŸ’–
  2. Returns a pointer to the object owned by *this, i.e. get(). πŸŽ€

πŸ’… Exceptions πŸ’…

  1. May throw if pointer has a throwing operator*. 😬

🧚 Notes 🧚

The use of std::add_lvalue_reference is to make it possible to instantiate std::unique_ptr<void> since void& isn't allowed in C++ while std::add_lvalue_reference<void> produces void. See LWG673 for details. iykyk πŸ’œ

πŸ”₯ Example πŸ”₯

// Run this code

#include <iostream>
#include <memory>

struct Foo
{
    void bar() { std::cout << "Foo::bar\n"; }
};

void f(const Foo&)
{
    std::cout << "f(const Foo&)\n";
}

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

    ptr->bar();
    f(*ptr);
}

πŸ“ Output πŸ“

Foo::bar
f(const Foo&)

πŸ‘‘ Defect reports πŸ‘‘

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR: LWG 2762 β€” Applied to: C++11 β€” Behavior as published: operator* might be potentially-throwing even if *get() was noexcept β€” Correct behavior: a conditional exception specification added. 🌈

🌟 See also 🌟

  • get β€” returns a pointer to the managed object (public member function). 🫢

That's the whole page, bestie β€” source: http://cppreference.com, version/date: 2024.06.10, and the header/name vibe is std::unique...:operator->(3) from the C++ Standard Libary manual. mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from