memory_uses_allocator
memory_uses_allocator (std::uses_allocator)
<memory> 헤더에 정의되어 있어요. T가 Alloc 타입을 사용하는지(즉 할당자 인식 타입인지) 검사하는 특성(trait)이에요. C++11부터 도입됐어요.
출처: cppreference
본문
template< class T, class Alloc >
struct uses_allocator;
(C++11부터)
T가 Alloc에서 변환 가능한 중첩 타입 allocator_type을 가지면 멤버 상수 value는 true예요. 그렇지 않으면 false예요.
헬퍼 변수 템플릿
template< class T, class Alloc >
constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
std::uses_allocator<T, Alloc>가 true인 타입 T는 uses-allocator construction(할당자 인식 생성)에 참여할 수 있어요. 이 특성은 std::scoped_allocator_adaptor와 std::pmr 다형성 할당자 메커니즘에서 "T가 할당자를 생성자에 전달받는 타입인지" 판단하는 데 쓰여요.
struct MyVec {
using allocator_type = std::pmr::polymorphic_allocator<int>;
explicit MyVec(std::pmr::polymorphic_allocator<int> a = {}) : alloc(a) {}
std::pmr::polymorphic_allocator<int> alloc;
};
static_assert(std::uses_allocator_v<MyVec, std::pmr::polymorphic_allocator<int>>);
주의 (Notes)
C++17 이전에는 T::allocator_type가 Alloc로 변환 가능한지 is_convertible를 이용해 검사했다. 이후 스펙이 다듬어졌어요.