Files
2025-10-25 03:02:53 +03:00

5.1 KiB
Raw Permalink Blame History

[unique.ptr.single.general]

20 Memory management library [mem]

20.3 Smart pointers [smartptr]

20.3.1 Unique-ownership pointers [unique.ptr]

20.3.1.3 unique_ptr for single objects [unique.ptr.single]

20.3.1.3.1 General [unique.ptr.single.general]

🔗

namespace std {template<class T, class D = default_delete> class unique_ptr {public:using pointer = see below; using element_type = T; using deleter_type = D; // [unique.ptr.single.ctor], constructorsconstexpr unique_ptr() noexcept; constexpr explicit unique_ptr(type_identity_t p) noexcept; constexpr unique_ptr(type_identity_t p, see below d1) noexcept; constexpr unique_ptr(type_identity_t p, see below d2) noexcept; constexpr unique_ptr(unique_ptr&& u) noexcept; constexpr unique_ptr(nullptr_t) noexcept; template<class U, class E>constexpr unique_ptr(unique_ptr<U, E>&& u) noexcept; // [unique.ptr.single.dtor], destructorconstexpr ~unique_ptr(); // [unique.ptr.single.asgn], assignmentconstexpr unique_ptr& operator=(unique_ptr&& u) noexcept; template<class U, class E>constexpr unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept; constexpr unique_ptr& operator=(nullptr_t) noexcept; // [unique.ptr.single.observers], observersconstexpr add_lvalue_reference_t operator*() const noexcept(see below); constexpr pointer operator->() const noexcept; constexpr pointer get() const noexcept; constexpr deleter_type& get_deleter() noexcept; constexpr const deleter_type& get_deleter() const noexcept; constexpr explicit operator bool() const noexcept; // [unique.ptr.single.modifiers], modifiersconstexpr pointer release() noexcept; constexpr void reset(pointer p = pointer()) noexcept; constexpr void swap(unique_ptr& u) noexcept; // disable copy from lvalue unique_ptr(const unique_ptr&) = delete; unique_ptr& operator=(const unique_ptr&) = delete; };}

1

#

A program that instantiates the definition of unique_ptr<T, D> is ill-formed if T* is an invalid type.

[Note 1:

This prevents the instantiation of specializations such asunique_ptr<T&, D> and unique_ptr<int() const, D>.

— end note]

2

#

The default type for the template parameter D isdefault_delete.

A client-supplied template argumentD shall be a function object type ([function.objects]), lvalue reference to function, or lvalue reference to function object type for which, given a value d of type D and a valueptr of type unique_ptr<T, D>::pointer, the expressiond(ptr) is valid and has the effect of disposing of the pointer as appropriate for that deleter.

3

#

If the deleter's type D is not a reference type, D shall meet the Cpp17Destructible requirements (Table 35).

4

#

If the qualified-id remove_reference_t::pointer is valid and denotes a type ([temp.deduct]), then unique_ptr<T, D>::pointer shall be a synonym for remove_reference_t::pointer.

Otherwiseunique_ptr<T, D>::pointer shall be a synonym for element_type*.

The type unique_ptr<T, D>::pointer shall meet the Cpp17NullablePointer requirements (Table 36).

5

#

[Example 1:

Given an allocator type X ([allocator.requirements.general]) and letting A be a synonym for allocator_traits, the types A::pointer,A::const_pointer, A::void_pointer, and A::const_void_pointer may be used as unique_ptr<T, D>::pointer.

— end example]