CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 23 Jul 2025 08:01:21 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/memory/unique_ptr/unique_ptr/
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 23 Jul 2025 08:01:21 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"a191-b0V6gq4HLIklqLzl3vlBEktF4cU"
Content-Encoding: gzip
Constructs a unique_ptr object, depending on the signature used:
The specialization of unique_ptr for arrays with runtime length (unique_ptr<T[],D>) does not support constructors (7) and (8), and does not accept types convertible to pointer (except pointer itself) as argument p (most notably pointers to types derived from element_type are not accepted).
Output:
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
-
<cfenv> (fenv.h)C++11
- <cfloat> (float.h)
-
<cinttypes> (inttypes.h)C++11
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
-
<cstdbool> (stdbool.h)C++11
- <cstddef> (stddef.h)
-
<cstdint> (stdint.h)C++11
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
-
<ctgmath> (tgmath.h)C++11
- <ctime> (time.h)
-
<cuchar> (uchar.h)C++11
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
-
<array>C++11
- <deque>
-
<forward_list>C++11
- <list>
- <map>
- <queue>
- <set>
- <stack>
-
<unordered_map>C++11
-
<unordered_set>C++11
- <vector>
-
Input/Output:
Multi-threading:
-
<atomic>C++11
-
<condition_variable>C++11
-
<future>C++11
-
<mutex>C++11
-
<thread>C++11
-
Other:
- <algorithm>
- <bitset>
-
<chrono>C++11
-
<codecvt>C++11
- <complex>
- <exception>
- <functional>
-
<initializer_list>C++11
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
-
<random>C++11
-
<ratio>C++11
-
<regex>C++11
- <stdexcept>
- <string>
-
<system_error>C++11
-
<tuple>C++11
-
<type_traits>C++11
-
<typeindex>C++11
- <typeinfo>
- <utility>
- <valarray>
<memory>
classes
- allocator
-
allocator_arg_tC++11
-
allocator_traitsC++11
- auto_ptr
- auto_ptr_ref
-
bad_weak_ptrC++11
-
default_deleteC++11
-
enable_shared_from_thisC++11
-
owner_lessC++11
-
pointer_traitsC++11
- raw_storage_iterator
-
shared_ptrC++11
-
unique_ptrC++11
-
uses_allocatorC++11
-
weak_ptrC++11
enum classes
-
pointer_safetyC++11
-
functions
-
addressofC++11
-
alignC++11
-
allocate_sharedC++11
-
const_pointer_castC++11
-
declare_no_pointersC++11
-
declare_reachableC++11
-
dynamic_pointer_castC++11
-
get_deleterC++11
-
get_pointer_safetyC++11
- get_temporary_buffer
-
make_sharedC++11
- return_temporary_buffer
-
static_pointer_castC++11
-
undeclare_no_pointersC++11
-
undeclare_reachableC++11
- uninitialized_copy
-
uninitialized_copy_nC++11
- uninitialized_fill
- uninitialized_fill_n
-
objects
-
allocator_argC++11
-
unique_ptr
-
unique_ptr::~unique_ptrC++11
-
unique_ptr::unique_ptrC++11
member functions
-
unique_ptr::getC++11
-
unique_ptr::get_deleterC++11
-
unique_ptr::operator boolC++11
-
unique_ptr::operator->C++11
-
unique_ptr::operator[]C++11
-
unique_ptr::operator*C++11
-
unique_ptr::operator=C++11
-
unique_ptr::releaseC++11
-
unique_ptr::resetC++11
-
unique_ptr::swapC++11
-
non-member overloads
-
relational operators (unique_ptr)C++11
-
swap (unique_ptr)C++11
-
- Reference
- <memory>
- unique_ptr
- unique_ptr
public member function
<memory>
std::unique_ptr::unique_ptr
default (1) | constexpr unique_ptr() noexcept; |
---|---|
from null pointer (2) | constexpr unique_ptr (nullptr_t) noexcept : unique_ptr() {} |
from pointer (3) | explicit unique_ptr (pointer p) noexcept; |
from pointer + lvalue deleter (4) | unique_ptr (pointer p, typename conditional<is_reference<D>::value,D,const D&> del) noexcept; |
from pointer + rvalue deleter (5) | unique_ptr (pointer p, typename remove_reference<D>::type&& del) noexcept; |
move (6) | unique_ptr (unique_ptr&& x) noexcept; |
move-cast (7) | template <class U, class E> unique_ptr (unique_ptr<U,E>&& x) noexcept; |
move from auto_ptr (8) | template <class U> unique_ptr (auto_ptr<U>&& x) noexcept; |
copy (deleted!) (9) | unique_ptr (const unique_ptr&) = delete; |
Construct unique_ptr
- default constructor (1), and (2)
- The object is empty (owns nothing), with value-initialized stored pointer and stored deleter.
- construct from pointer (3)
- The object takes ownership of p, initializing its stored pointer to p and value-initializing its stored deleter.
- construct from pointer + lvalue deleter (4)
- The object takes ownership of p, initializing its stored pointer to p, and using a copy of d as deleter (unless D is a reference type, in which case, d is used directly).
- construct from pointer + rvalue deleter (5)
- The object takes ownership of p, initializing its stored pointer to p and moving d into the object to be used as deleter.
- move constructors (6) and (7)
- The object acquires the content managed by x, moving into the object both its stored pointer (of which it takes ownership) and its stored deleter (unless x's deleter type is a reference, in which case the deleter is copied instead of moved).
- construct by moving auto_ptr (8)
- The object acquires the content managed by x, initializing its stored pointer as if by calling x.release() (taking ownership of the pointer), and value-initializing its stored deleter.
- copy construction (9)
- Copy construction is disabled for objects of type unique_ptr (see move constructors, 6 and 7).
The specialization of unique_ptr for arrays with runtime length (unique_ptr<T[],D>) does not support constructors (7) and (8), and does not accept types convertible to pointer (except pointer itself) as argument p (most notably pointers to types derived from element_type are not accepted).
Parameters
- p
- Pointer whose ownership is taken over by the object.
This pointer value shall not be already managed by any other managed pointer (i.e., this value shall not come from calling member get on a managed pointer).
pointer is a member type, defined as the pointer type that points to the type of object managed.
- del
- Deleter object to be used to release the owned object.
D refers to unique_ptr's second template parameter, which describes its deleter type. - x
- A unique_ptr or auto_ptr object.
U* shall be implicitly convertible to T* (where T is unique_ptr's first template parameter).
Example
|
|
Output:
u1: null u2: null u3: not null u4: not null u5: null u6: null u7: not null u8: not null |
See also
- unique_ptr::operator=
- unique_ptr assignment (public member function)
- unique_ptr::reset
- Reset pointer (public member function)
- unique_ptr::~unique_ptr
- Destroy unique_ptr (public member function)
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us