std::unique_ptr<T,Deleter>::operator*, std::unique_ptr<T,Deleter>::operator->
From cppreference.com
< cpp | memory | unique ptr
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Memory management library
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
Garbage collector support (until C++23)
(exposition only*)
(C++11)
(C++23)
(C++11)
(C++17)
(C++11)
(C++11)
(C++20)
(C++20)
(C++17)
(C++11)
(C++17)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(until C++20*)
(until C++20*)
(until C++20*)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)
(C++11)
(C++17)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
(until C++17*)
(C++11)
(C++17)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++11)
(C++20)
(C++11)
(C++11)
(C++20)
(C++26)
std::unique_ptr
unique_ptr::operator*unique_ptr::operator->
(C++14)(C++20)
(until C++20)(C++20)
(C++20)
typename std::add_lvalue_reference <T>::type operator*() const
noexcept(noexcept(*std::declval <pointer>()));
(1)
(since C++11) noexcept(noexcept(*std::declval <pointer>()));
(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.
These member functions are only provided for unique_ptr
for the single objects i.e. the primary template.
1) If std::reference_converts_from_temporary_v
<std::add_lvalue_reference_t <T>,
decltype(*std::declval <pointer>())> is true, the program is ill-formed.
(since C++23)<std::add_lvalue_reference_t <T>,
decltype(*std::declval <pointer>())> is true, the program is ill-formed.
If get() is a null pointer, the behavior is undefined.
[edit] 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().
[edit] Exceptions
1) May throw if
pointer
has a throwing operator*.[edit] 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.
[edit] 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&)
[edit] Defect reports
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 |
added a conditional exception specification |
LWG 4148 | C++23 | operator* could return a dangling reference ifelement_type* differs from Deleter::pointer
|
the program is ill- formed in this case |