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

8.8 KiB
Raw Permalink Blame History

[unique.ptr.runtime]

20 Memory management library [mem]

20.3 Smart pointers [smartptr]

20.3.1 Unique-ownership pointers [unique.ptr]

20.3.1.4 unique_ptr for array objects with a runtime length [unique.ptr.runtime]

20.3.1.4.1 General [unique.ptr.runtime.general]

🔗

namespace std {template<class T, class D> class unique_ptr<T[], D> {public:using pointer = see below; using element_type = T; using deleter_type = D; // [unique.ptr.runtime.ctor], constructorsconstexpr unique_ptr() noexcept; template constexpr explicit unique_ptr(U p) noexcept; template constexpr unique_ptr(U p, see below d) noexcept; template constexpr unique_ptr(U p, see below d) noexcept; constexpr unique_ptr(unique_ptr&& u) noexcept; template<class U, class E>constexpr unique_ptr(unique_ptr<U, E>&& u) noexcept; constexpr unique_ptr(nullptr_t) noexcept; // destructorconstexpr ~unique_ptr(); // 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.runtime.observers], observersconstexpr T& operator[](size_t i) const; 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.runtime.modifiers], modifiersconstexpr pointer release() noexcept; template constexpr void reset(U p) noexcept; constexpr void reset(nullptr_t = nullptr) 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 specialization for array types is provided with a slightly altered interface.

  • (1.1)

    Conversions between different types ofunique_ptr<T[], D> that would be disallowed for the corresponding pointer-to-array types, and conversions to or from the non-array forms ofunique_ptr, produce an ill-formed program.

  • (1.2)

    Pointers to types derived from T are rejected by the constructors, and by reset.

  • (1.3)

    The observers operator* andoperator-> are not provided.

  • (1.4)

    The indexing observer operator[] is provided.

  • (1.5)

    The default deleter will call delete[].

2

#

Descriptions are provided below only for members that differ from the primary template.

3

#

The template argument T shall be a complete type.

20.3.1.4.2 Constructors [unique.ptr.runtime.ctor]

🔗

template<class U> constexpr explicit unique_ptr(U p) noexcept;

1

#

This constructor behaves the same as the constructor in the primary template that takes a single parameter of type pointer.

2

#

Constraints:

U is the same type as pointer, or

pointer is the same type as element_type*,U is a pointer type V*, andV()[] is convertible to element_type()[].

🔗

template<class U> constexpr unique_ptr(U p, see below d) noexcept; template<class U> constexpr unique_ptr(U p, see below d) noexcept;

3

#

These constructors behave the same as the constructors in the primary template that take a parameter of type pointer and a second parameter.

4

#

Constraints:

U is the same type as pointer,

U is nullptr_t, or

pointer is the same type as element_type*, U is a pointer type V*, and V()[] is convertible to element_type()[].

🔗

template<class U, class E> constexpr unique_ptr(unique_ptr<U, E>&& u) noexcept;

5

#

This constructor behaves the same as in the primary template.

6

#

Constraints: Where UP is unique_ptr<U, E>:

U is an array type, and

pointer is the same type as element_type*, and

UP::pointer is the same type as UP::element_type*, and

UP::element_type()[] is convertible to element_type()[], and

either D is a reference type and E is the same type as D, or D is not a reference type and E is implicitly convertible to D.

[Note 1:

This replaces the Constraints: specification of the primary template.

— end note]

20.3.1.4.3 Assignment [unique.ptr.runtime.asgn]

🔗

template<class U, class E> constexpr unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;

1

#

This operator behaves the same as in the primary template.

2

#

Constraints: Where UP is unique_ptr<U, E>:

U is an array type, and

pointer is the same type as element_type*, and

UP::pointer is the same type as UP::element_type*, and

UP::element_type()[] is convertible to element_type()[], and

is_assignable_v<D&, E&&> is true.

[Note 1:

This replaces the Constraints: specification of the primary template.

— end note]

20.3.1.4.4 Observers [unique.ptr.runtime.observers]

🔗

constexpr T& operator[](size_t i) const;

1

#

Preconditions: i < the number of elements in the array to which the stored pointer points.

2

#

Returns: get()[i].

20.3.1.4.5 Modifiers [unique.ptr.runtime.modifiers]

🔗

constexpr void reset(nullptr_t p = nullptr) noexcept;

1

#

Effects: Equivalent to reset(pointer()).

🔗

template<class U> constexpr void reset(U p) noexcept;

2

#

This function behaves the same as the reset member of the primary template.

3

#

Constraints:

U is the same type as pointer, or

pointer is the same type as element_type*, U is a pointer type V*, and V()[] is convertible to element_type()[].