Namespaces
Variants

std::unique_ptr<T,Deleter>:: reset

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (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) (until C++23)
기본 템플릿 unique_ptr<T>의 멤버
void reset ( pointer ptr = pointer ( ) ) noexcept ;
(1) (C++23부터 constexpr)
특수화 unique_ptr<T[]>의 멤버
template < class U >
void reset ( U ptr ) noexcept ;
(2) (C++23부터 constexpr)
void reset ( std:: nullptr_t = nullptr ) noexcept ;
(3) (C++23부터 constexpr)

관리되는 객체를 대체합니다.

1,2) 다음에 해당함 auto old_ptr = get ( ) ;
/* assigns “ptr” to the stored pointer */
if ( old_ptr )
get_deleter ( ) ( old_ptr ) ;
.
만약 get_deleter ( ) ( old_ptr ) 가 예외를 던지면, 동작은 정의되지 않습니다.
2) 이 오버로드는 다음 조건 중 하나를 만족할 때만 오버로드 해결에 참여합니다:
  • pointer element_type* 와 동일한 타입인 경우
  • 다음의 모든 조건이 충족되는 경우:
    • pointer element_type* 와 동일한 타입
    • U 가 포인터 타입 V* 이며, V(*)[] element_type(*)[] 로 변환 가능한 경우
3) 다음에 해당함: reset ( pointer ( ) ) .

목차

매개변수

ptr - 관리할 새 객체에 대한 포인터

참고 사항

새로운 삭제자를 함께 제공하면서 관리 객체를 교체하려면, 이동 할당 연산자를 사용할 수 있습니다.

자가 리셋(self-reset)에 대한 테스트, 즉 ptr 가 이미 * this 에 의해 관리되는 객체를 가리키는지 여부에 대한 검사는 수행되지 않으며, 컴파일러 확장 기능이나 디버깅 어설트(assert)로 제공되는 경우를 제외하고는 검사되지 않습니다. p. reset ( p. release ( ) ) 와 같은 코드는 자가 리셋을 포함하지 않으며, p. reset ( p. get ( ) ) 와 같은 코드만이 자가 리셋을 포함합니다.

예제

#include <iostream>
#include <memory>
struct Foo // 관리할 객체
{
    Foo() { std::cout << "Foo...\n"; }
    ~Foo() { std::cout << "~Foo...\n"; }
};
struct D // 삭제자
{
    void operator() (Foo* p)
    {
        std::cout << "Calling delete for Foo object... \n";
        delete p;
    }
};
int main()
{
    std::cout << "Creating new Foo...\n";
    std::unique_ptr<Foo, D> up(new Foo(), D()); // up이 Foo 포인터 소유 (삭제자 D)
    std::cout << "Replace owned Foo with a new Foo...\n";
    up.reset(new Foo());  // 이전 객체에 대해 삭제자 호출
    std::cout << "Release and delete the owned Foo...\n";
    up.reset(nullptr);      
}

출력:

Creating new Foo...
Foo...
Replace owned Foo with a new Foo...
Foo...
Calling delete for Foo object...
~Foo...
Release and delete the owned Foo...
Calling delete for Foo object...
~Foo...

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2118 C++11 unique_ptr<T[]>::reset 한정 변환을 거부함 허용함
LWG 2169 C++11 오버로드 unique_ptr<T[]>::reset(pointer) 가 존재함 오버로드를 제거함

참고 항목

관리되는 객체에 대한 포인터를 반환하고 소유권을 해제합니다
(public member function)