indirect — std::indirect

indirect — std::indirect

std::indirect동적 할당된 객체를 value-like(값과 같은) 의미론으로 감싸는(wrapper) 타입이에요. C++26에서 도입됐어요.

std::pmr::indirect는 다형성 할당자(polymorphic allocator)를 사용하는 별칭 템플릿이에요.

출처: cppreference

본문

// <memory> 헤더, C++26
template< class T, class Allocator = std::allocator<T> >
class indirect;

namespace pmr {
    template< class T >
    using indirect = std::indirect<T, std::pmr::polymorphic_allocator<T>>;
}
  • (1) std::indirect는 value-like 의미론을 가진, 동적 할당 객체를 담는 래퍼예요.
  • (2) std::pmr::indirect는 다형성 할당자를 쓰는 별칭 템플릿이에요.

std::indirect는 포인터 기반이지만 값처럼 동작해요. 복사, 이동, 비교, 그리고 indirect_value 같은 연산을 제공해요.

#include <memory>

std::indirect<int> a;          // 값 초기화된 int 보유
a = 42;                        // 할당
int n = *a;                    // 역참조 → 42

std::indirect<int> b = a;      // 복사 → 독립된 값
b = 100;
// a는 42, b는 100 (깊은 복사)

특징

  • value-like 의미론 — 복사하면 동적 객체도 깊게 복사돼 독립적이에요.
  • 이동 연산 — 이동 시 포인터를 옮겨 효율적이에요.
  • 타입 안전 역참조*, ->, std::indirect_value 지원.
  • 할당자 지원 — Allocator로 객체의 할당을 제어할 수 있어요.
struct Point { int x, y; };
std::indirect<Point> p;
p->x = 1; p->y = 2;    // -> 연산자

indirect는 "값처럼 복사/이동되지만 내부는 힙에 하나만 있는" 불변 객체를 만들 때, PAO(pimpl 같은) 패턴이나 재귀 타입을 값 의미론으로 다루고 싶을 때 유용해요. C++26 신규 기능이라 최신 표준 라이브러리 지원이 필요해요.

더 알아보기 (Learn more)

cppreference