std::unique...:operator->(3)
π 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 β¨
- Returns the object owned by
*this, equivalent to*get(). π - Returns a pointer to the object owned by
*this, i.e.get(). π
π Exceptions π
- May throw if
pointerhas a throwingoperator*. π¬
π§ 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
- 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