std::shared_ptr<T>:: reset
|
void
reset
(
)
noexcept
;
|
(1) | (C++11부터) |
|
template
<
class
Y
>
void reset ( Y * ptr ) ; |
(2) | (C++11부터) |
|
template
<
class
Y,
class
Deleter
>
void reset ( Y * ptr, Deleter d ) ; |
(3) | (C++11부터) |
|
template
<
class
Y,
class
Deleter,
class
Alloc
>
void reset ( Y * ptr, Deleter d, Alloc alloc ) ; |
(4) | (C++11부터) |
관리되는 객체를
ptr
이 가리키는 객체로 대체합니다. 선택적 삭제자
d
를 제공할 수 있으며, 이는 이후 더 이상
shared_ptr
객체가 소유하지 않을 때 새로운 객체를 파괴하는 데 사용됩니다. 기본적으로
delete
표현식이 삭제자로 사용됩니다. 제공된 타입에 해당하는 적절한
delete
표현식이 항상 선택되며, 이것이 별도의 매개변수
Y
를 사용하는 템플릿으로 함수가 구현된 이유입니다.
만약
*
this
가 이미 객체를 소유하고 있으며 이것이 해당 객체를 소유하는 마지막
shared_ptr
인 경우, 객체는 소유된 삭제자를 통해 파괴됩니다.
ptr 가 가리키는 객체가 이미 소유된 상태라면, 이 함수는 일반적으로 미정의 동작을 초래합니다.
Y
는 완전한 타입이어야 하며
T
로 암시적으로 변환 가능해야 합니다. 추가적으로:
Deleter
는
T
타입에 대해 호출 가능해야 합니다. 즉,
d
(
ptr
)
가 올바른 형식이어야 하며, 잘 정의된 동작을 가지고 어떠한 예외도 던지지 않아야 합니다.
Deleter
는
CopyConstructible
여야 하며, 복사 생성자와 소멸자는 예외를 던지지 않아야 합니다. 다음 코드와 동등합니다:
shared_ptr
<
T
>
(
ptr, d
)
.
swap
(
*
this
)
;
.
Alloc
은
Allocator
여야 합니다. 복사 생성자와 소멸자는 예외를 던지지 않아야 합니다. 다음 코드와 동등합니다:
shared_ptr
<
T
>
(
ptr, d, alloc
)
.
swap
(
*
this
)
;
.
목차 |
매개변수
| ptr | - | 소유권을 획득할 객체에 대한 포인터 |
| d | - | 객체 삭제를 위해 저장할 삭제자 |
| alloc | - | 내부 할당에 사용할 할당자 |
반환값
(없음)
예외
예제
#include <iostream> #include <memory> struct Foo { Foo(int n = 0) noexcept : bar(n) { std::cout << "Foo::Foo(), bar = " << bar << " @ " << this << '\n'; } ~Foo() { std::cout << "Foo::~Foo(), bar = " << bar << " @ " << this << '\n'; } int getBar() const noexcept { return bar; } private: int bar; }; int main() { std::cout << "1) 유일한 소유권\n"; { std::shared_ptr<Foo> sptr = std::make_shared<Foo>(100); std::cout << "Foo::bar = " << sptr->getBar() << ", use_count() = " << sptr.use_count() << '\n'; // shared_ptr를 Foo의 새 인스턴스 없이 재설정합니다. // 이 호출 후 기존 인스턴스는 소멸됩니다. std::cout << "sptr.reset() 호출...\n"; sptr.reset(); // 여기서 Foo의 소멸자를 호출합니다 std::cout << "reset() 이후: use_count() = " << sptr.use_count() << ", sptr = " << sptr << '\n'; } // Foo의 소멸자 호출 없음, 이전에 reset()에서 이미 수행됨. std::cout << "\n2) 고유 소유권\n"; { std::shared_ptr<Foo> sptr = std::make_shared<Foo>(200); std::cout << "Foo::bar = " << sptr->getBar() << ", use_count() = " << sptr.use_count() << '\n'; // shared_ptr를 재설정하고 Foo의 새 인스턴스를 전달합니다. // 이 호출 후 기존 인스턴스는 소멸됩니다. std::cout << "call sptr.reset()...\n"; sptr.reset(new Foo{222}); std::cout << "reset() 이후: use_count() = " << sptr.use_count() << ", sptr = " << sptr << "\n스코프를 벗어나는 중...\n"; } // Foo의 소멸자를 호출합니다. std::cout << "\n3) 다중 소유권\n"; { std::shared_ptr<Foo> sptr1 = std::make_shared<Foo>(300); std::shared_ptr<Foo> sptr2 = sptr1; std::shared_ptr<Foo> sptr3 = sptr2; std::cout << "Foo::bar = " << sptr1->getBar() << ", use_count() = " << sptr1.use_count() << '\n'; // shared_ptr sptr1을 재설정하고 Foo의 새 인스턴스를 할당합니다. // 이전 인스턴스는 sptr2와 sptr3 사이에서 계속 공유됩니다. std::cout << "call sptr1.reset()...\n"; sptr1.reset(new Foo{333}); std::cout << "reset() 후:\n" << "sptr1.use_count() = " << sptr1.use_count() << ", sptr1 @ " << sptr1 << '\n' << "sptr2.use_count() = " << sptr2.use_count() << ", sptr2 @ " << sptr2 << '\n' << "sptr3.use_count() = " << sptr3.use_count() << ", sptr3 @ " << sptr3 << '\n' << "스코프를 벗어나는 중...\n"; } // 두 개의 소멸자를 호출합니다: 1) sptr1이 소유한 Foo의, // 2) sptr2/sptr3 간에 공유되는 Foo. }
가능한 출력:
1) 단일 소유권 Foo::Foo(), bar = 100 @ 0x23c5040 Foo::bar = 100, use_count() = 1 sptr.reset() 호출 중... Foo::~Foo(), bar = 100 @ 0x23c5040 reset() 후: use_count() = 0, sptr = 0 2) 단일 소유권 Foo::Foo(), bar = 200 @ 0x23c5040 Foo::bar = 200, use_count() = 1 sptr.reset() 호출 중... Foo::Foo(), bar = 222 @ 0x23c5050 Foo::~Foo(), bar = 200 @ 0x23c5040 reset() 후: use_count() = 1, sptr = 0x23c5050 범위를 벗어나는 중... Foo::~Foo(), bar = 222 @ 0x23c5050 3) 다중 소유권 Foo::Foo(), bar = 300 @ 0x23c5080 Foo::bar = 300, use_count() = 3 sptr1.reset() 호출 중... Foo::Foo(), bar = 333 @ 0x23c5050 reset() 후: sptr1.use_count() = 1, sptr1 @ 0x23c5050 sptr2.use_count() = 2, sptr2 @ 0x23c5080 sptr3.use_count() = 2, sptr3 @ 0x23c5080 범위를 벗어나는 중... Foo::~Foo(), bar = 300 @ 0x23c5080 Foo::~Foo(), bar = 333 @ 0x23c5050
참고 항목
새로운
shared_ptr
을 생성함
(public member function) |