Namespaces
Variants

std::weak_ptr<T>:: 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)
void reset ( ) noexcept ;
(C++11부터)

관리되는 객체에 대한 참조를 해제합니다. 호출 후 * this 는 어떤 객체도 관리하지 않습니다.

목차

매개변수

(없음)

반환값

(없음)

예제

#include <iostream>
#include <memory>
int main()
{
    auto shared = std::make_shared<int>(), shared2 = shared, shared3 = shared2;
    auto weak = std::weak_ptr<int>{shared};
    std::cout << std::boolalpha 
              << "shared.use_count(): " << shared.use_count() << '\n'
              << "weak.use_count(): " << weak.use_count() << '\n'
              << "weak.expired(): " << weak.expired() << '\n';
    weak.reset();
    std::cout << "weak.reset();\n"
              << "shared.use_count(): " << shared.use_count() << '\n'
              << "weak.use_count(): " << weak.use_count() << '\n'
              << "weak.expired(): " << weak.expired() << '\n';
}

출력:

shared.use_count(): 3
weak.use_count(): 3
weak.expired(): false
weak.reset();
shared.use_count(): 3
weak.use_count(): 0
weak.expired(): true

참고 항목

참조된 객체가 이미 삭제되었는지 확인합니다
(public member function)