Namespaces
Variants

std:: enable_shared_from_this

From ko.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 >
class enable_shared_from_this ;
(C++11부터)

std::enable_shared_from_this 는 현재 std::shared_ptr pt 에 의해 관리되는 객체 t 가 pt 와 객체 t 의 소유권을 공유하는 추가 std::shared_ptr 인스턴스들( pt1 , pt2 등)을 안전하게 생성할 수 있도록 합니다.

std::enable_shared_from_this<T> 에서 공개적으로 상속받으면 T 타입에 shared_from_this 멤버 함수가 제공됩니다. T 타입의 객체 t 가 std:: shared_ptr < T > 인 pt 에 의해 관리되는 경우, T::shared_from_this 를 호출하면 t 와 pt 가 소유권을 공유하는 새로운 std:: shared_ptr < T > 를 반환합니다.

목차

데이터 멤버

멤버 설명
mutable std:: weak_ptr < T > weak_this 이 객체의 첫 번째 공유 소유자의 제어 블록을 추적하는 객체 * this
( 설명 전용 멤버 객체* )

멤버 함수

enable_shared_from_this 객체를 생성합니다
(protected member function)
enable_shared_from_this 객체를 파괴합니다
(protected member function)
* this 에 대한 참조를 반환합니다
(protected member function)
* this 의 소유권을 공유하는 std::shared_ptr 을 반환합니다
(public member function)
* this 의 소유권을 공유하는 std::weak_ptr 을 반환합니다
(public member function)

참고

의 생성자는 std::shared_ptr 모호하지 않고 접근 가능한(즉, public) enable_shared_from_this기반 클래스의 존재를 감지하고 새로 생성된 std::shared_ptr를 weak_this에 할당합니다(만약 이미 살아있는 std::shared_ptr에 의해 소유되지 않은 경우). 다른 std::shared_ptr에 의해 이미 관리되는 객체에 대해 std::shared_ptr를 생성하면 weak_this와의 소유권을 업데이트하거나 공유하지 않으므로 정의되지 않은 동작이 발생합니다.

호출 shared_from_this은 std::shared_ptr<T>에 의해 관리되지 않는 객체에 대해 std::bad_weak_ptr을(를) 던집니다(왜냐하면 weak_this  멤버가 "expired" 상태에 있고, 만료된 std::shared_ptr로부터 std::weak_ptr를 생성하면 예외가 발생하기 때문입니다).

enable_shared_from_this는 std::shared_ptr<T>(this)와 같은 표현식에 대한 안전한 대안을 제공합니다. 이는 this가 서로를 인식하지 못하는 여러 소유자에 의해 두 번 이상 파괴될 가능성이 있습니다(아래 예제 참조).

기능 테스트 매크로 값 표준 기능
__cpp_lib_constexpr_memory 202506L (C++26) Constexpr std::enable_shared_from_this

예제

#include <iostream>
#include <memory>
class Good : public std::enable_shared_from_this<Good>
{
public:
    std::shared_ptr<Good> getptr()
    {
        return shared_from_this();
    }
};
class Best : public std::enable_shared_from_this<Best>
{
    struct Private{ explicit Private() = default; };
public:
    // 생성자는 이 클래스에서만 사용 가능
    Best(Private) {}
    // 다른 모든 사용자는 이 팩토리 함수를 사용해야 함
    // 따라서 모든 Best 객체는 shared_ptr에 포함됨
    static std::shared_ptr<Best> create()
    {
        return std::make_shared<Best>(Private());
    }
    std::shared_ptr<Best> getptr()
    {
        return shared_from_this();
    }
};
struct Bad
{
    std::shared_ptr<Bad> getptr()
    {
        return std::shared_ptr<Bad>(this);
    }
    ~Bad() { std::cout << "Bad::~Bad() called\n"; }
};
void testGood()
{
    // Good: 두 shared_ptr이 동일한 객체를 공유함
    std::shared_ptr<Good> good0 = std::make_shared<Good>();
    std::shared_ptr<Good> good1 = good0->getptr();
    std::cout << "good1.use_count() = " << good1.use_count() << '\n';
}
void misuseGood()
{
    // Bad: std::shared_ptr이 호출자를 소유하지 않은 상태에서 shared_from_this가 호출됨
    try
    {
        Good not_so_good;
        std::shared_ptr<Good> gp1 = not_so_good.getptr();
    }
    catch (std::bad_weak_ptr& e)
    {
        // 정의되지 않은 동작 (C++17 이전) 및 std::bad_weak_ptr 예외 발생 (C++17 이후)
        std::cout << e.what() << '\n';
    }
}
void testBest()
{
    // Best: 동일하지만 스택 할당이 불가능함:
    std::shared_ptr<Best> best0 = Best::create();
    std::shared_ptr<Best> best1 = best0->getptr();
    std::cout << "best1.use_count() = " << best1.use_count() << '\n';
    // Best stackBest; // <- Best::Best()가 private이므로 컴파일되지 않음.
}
void testBad()
{
    // Bad: 각 shared_ptr이 객체의 유일한 소유자라고 생각함
    std::shared_ptr<Bad> bad0 = std::make_shared<Bad>();
    std::shared_ptr<Bad> bad1 = bad0->getptr();
    std::cout << "bad1.use_count() = " << bad1.use_count() << '\n';
} // UB: Bad의 이중 삭제
int main()
{
    testGood();
    misuseGood();
    testBest();
    testBad();
}

가능한 출력:

good1.use_count() = 2
bad_weak_ptr
best1.use_count() = 2
bad1.use_count() = 1
Bad::~Bad() 호출됨
Bad::~Bad() 호출됨
*** glibc 감지됨 *** ./test: 이중 해제 또는 손상

결함 보고서

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

DR 적용 대상 게시된 동작 올바른 동작
LWG 2179
( P0033R1 )
C++11 enable_shared_from_this 로부터 파생된 타입 T 가 주어졌을 때,
동일한 T * 객체로부터 두 개의 std:: shared_ptr < T > 를 생성하는 동작이 불명확했음
이 경우 동작은
정의되지 않음
LWG 2529
( P0033R1 )
C++11 기본 std::weak_ptr 이 어떻게 업데이트되는지 불명확했음 명확히 규정됨

참고 항목

(C++11)
공유 객체 소유권 의미론을 가진 스마트 포인터
(클래스 템플릿)
새 객체를 관리하는 공유 포인터를 생성함
(함수 템플릿)