memory_indirect
memory_indirect (std::indirect)
<memory> 헤더에 정의되어 있어요. 동적으로 할당된 객체를 값 의미론(value-like semantics)으로 감싸는 래퍼예요. C++26부터 도입됐어요.
출처: cppreference
본문
template< class T, class Allocator = std::allocator<T> >
class indirect; // (1) since C++26
namespace pmr {
template< class T >
using indirect = std::indirect<T, std::pmr::polymorphic_allocator<T>>; // (2) since C++26
}
-
std::indirect는 값 의미론으로 동적으로 할당된 객체를 담는 래퍼예요.
-
std::pmr::indirect는 다형적 할당자를 사용하는 별칭 템플릿이에요.
std::indirect 객체는 소유한 객체의 수명을 관리해요. std::indirect 객체는 move된 이후에만 소유한 객체가 없을 수 있는데, 이 경우 valueless 상태예요.
std::indirect<T, Allocator> 타입의 모든 객체는 Allocator 타입의 객체를 사용해 소유한 객체를 할당·해제해요. 이는 값 의미론이라, 복사하면 깊은 복사로 새 객체를 만들고, move하면 소유권을 이전해요.
std::indirect<int> a = 10; // 동적 할당된 int 10 을 관리
std::indirect<int> b = a; // 깊은 복사 (b 는 별도 int)
std::indirect<int> c = std::move(a); // 이동 (a 는 valueless 가 됨)
예외
T가 불완전한 타입이면 생성·대입이 제한될 수 있어요. 할당자·T의 동작에 따라 예외가 발생할 수 있어요.