Namespaces
Variants

std::weak_ptr<T>:: weak_ptr

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)
constexpr weak_ptr ( ) noexcept ;
(1) (C++11부터)
weak_ptr ( const weak_ptr & r ) noexcept ;
(2) (C++11부터)
template < class Y >
weak_ptr ( const weak_ptr < Y > & r ) noexcept ;
(2) (C++11부터)
template < class Y >
weak_ptr ( const std:: shared_ptr < Y > & r ) noexcept ;
(2) (C++11부터)
weak_ptr ( weak_ptr && r ) noexcept ;
(3) (C++11부터)
template < class Y >
weak_ptr ( weak_ptr < Y > && r ) noexcept ;
(3) (C++11부터)

weak_ptr r 과 잠재적으로 객체를 공유할 수 있는 새로운 weak_ptr 를 생성합니다.

1) 기본 생성자. 빈 weak_ptr 을 생성합니다.
2) r 가 관리하는 객체를 공유하는 새로운 weak_ptr 을 생성합니다. 만약 r 이 객체를 관리하지 않는다면, * this 도 객체를 관리하지 않습니다. 템플릿화된 오버로드는 Y* T* 로 암시적으로 변환 가능하지 않으면 오버로드 해결에 참여하지 않습니다 , 또는 Y 가 어떤 타입 U 와 숫자 N 에 대해 "array of N U " 타입이고, T 가 "(possibly cv-qualified) U 의 unknown bound 배열" 타입인 경우 (C++17부터) .
3) 이동 생성자. r 에서 * this 로 weak_ptr 인스턴스를 이동합니다. 이후, r 는 비어 있고 r. use_count ( ) == 0 입니다. 템플릿화된 오버로드는 Y* T* 로 암시적으로 변환 가능하지 않으면 오버로드 해결에 참여하지 않습니다.

목차

매개변수

r - std::shared_ptr 또는 std::weak_ptr 을(를) 볼 std::weak_ptr

참고 사항

기본 생성자가 constexpr 이므로, 정적 std::weak_ptr 은 동적 비지역 초기화가 시작되기 전에 정적 비지역 초기화 의 일부로 초기화됩니다. 이로 인해 어떤 정적 객체의 생성자에서도 std::weak_ptr 을 안전하게 사용할 수 있습니다.

예제

#include <iostream>
#include <memory>
struct Foo {};
int main()
{
    std::weak_ptr<Foo> w_ptr;
    {
        auto ptr = std::make_shared<Foo>();
        w_ptr = ptr;
        std::cout << "w_ptr.use_count() inside scope: " << w_ptr.use_count() << '\n';
    }
    std::cout << "w_ptr.use_count() out of scope: " << w_ptr.use_count() << '\n';
    std::cout << "w_ptr.expired() out of scope: "
              << std::boolalpha << w_ptr.expired() << '\n';
}

출력:

w_ptr.use_count() inside scope: 1
w_ptr.use_count() out of scope: 0
w_ptr.expired() out of scope: true

결함 보고서

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

DR 적용 대상 게시된 동작 올바른 동작
LWG 2315 C++11 weak_ptr 에 대해 이동 의미론이 활성화되지 않음 활성화됨

참고 항목

weak_ptr 를 할당합니다
(public member function)