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
.
The behavior is undefined if get() == nullptr
.
These member functions are only provided for unique_ptr
for the single objects i.e. the primary template.
(none).
*this
, equivalent to *get()
.*this
, i.e. get()
.pointer
has a throwing operator*
.#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&)
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2762 | C++11 | operator* might be potentially-throwing even if*get() was noexcept | a conditional exception specification added |
returns a pointer to the managed object (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/memory/unique_ptr/operator*