Namespaces
Variants

deduction guides for std::shared_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)
헤더 파일에 정의됨 <memory>
template < class T >
shared_ptr ( std:: weak_ptr < T > ) - > shared_ptr < T > ;
(1) (C++17부터)
template < class T, class D >
shared_ptr ( std:: unique_ptr < T, D > ) - > shared_ptr < T > ;
(2) (C++17부터)

이러한 deduction guides 는 암시적 deduction guides가 놓친 엣지 케이스를 처리하기 위해 std::shared_ptr 에 제공됩니다.

배열 형태와 비배열 형태의 new 에서 얻은 포인터를 구분할 수 없기 때문에, 포인터 타입으로부터는 클래스 템플릿 인수 추론이 이루어지지 않는다는 점에 유의하십시오.

예제

#include <memory>
int main()
{
    auto p = std::make_shared<int>(42);
    std::weak_ptr w{p};    // 이 경우 명시적 추론 가이드가 사용됨
    std::shared_ptr p2{w}; // 이 경우 명시적 추론 가이드가 사용됨
}