Namespaces
Variants

std:: allocator

From 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 >
struct allocator ;
(1)
template <>
struct allocator < void > ;
(2) (C++17에서 사용 중단됨)
(C++20에서 제거됨)

std::allocator 클래스 템플릿은 사용자가 지정한 할당자가 제공되지 않을 경우 모든 표준 라이브러리 컨테이너가 사용하는 기본 Allocator 입니다. 기본 할당자는 상태를 가지지 않습니다, 즉 주어진 할당자의 모든 인스턴스는 상호 교환 가능하며, 동등하게 비교될 수 있고, 동일한 할당자 유형의 다른 인스턴스에 의해 할당된 메모리를 해제할 수 있습니다.

void 에 대한 명시적 특수화는 reference , const_reference , size_type , difference_type 멤버 typedef를 포함하지 않습니다. 이 특수화는 어떤 멤버 함수도 선언하지 않습니다.

(C++20 이전)

기본 할당자는 할당자 완전성 요구사항 을 충족합니다.

(C++17부터)

목차

멤버 타입

유형 정의
value_type T
pointer (C++17에서 사용 중단됨) (C++20에서 제거됨) T*
const_pointer (C++17에서 사용 중단됨) (C++20에서 제거됨) const T *
reference (C++17에서 사용 중단됨) (C++20에서 제거됨) T&
const_reference (C++17에서 사용 중단됨) (C++20에서 제거됨) const T &
size_type std::size_t
difference_type std::ptrdiff_t
propagate_on_container_move_assignment (C++11) std::true_type
rebind (C++17에서 사용 중단됨) (C++20에서 제거됨) template < class U >

struct rebind
{
typedef allocator < U > other ;
} ;

is_always_equal (C++11) (C++23에서 사용 중단됨) (C++26에서 제거됨) std::true_type

멤버 함수

새로운 allocator 인스턴스를 생성합니다
(public member function)
allocator 인스턴스를 소멸시킵니다
(public member function)
(until C++20)
operator & 가 오버로드된 경우에도 객체의 주소를 얻습니다
(public member function)
초기화되지 않은 저장 공간을 할당합니다
(public member function)
요청된 크기 이상의 초기화되지 않은 저장 공간을 할당합니다
(public member function)
저장 공간을 해제합니다
(public member function)
(until C++20)
지원되는 최대 할당 크기를 반환합니다
(public member function)
(until C++20)
할당된 저장 공간에 객체를 생성합니다
(public member function)
(until C++20)
할당된 저장 공간의 객체를 소멸시킵니다
(public member function)

비멤버 함수

(removed in C++20)
두 allocator 인스턴스를 비교합니다
(public member function)

참고 사항

멤버 템플릿 클래스 rebind 는 다른 타입에 대한 할당자를 얻는 방법을 제공합니다. 예를 들어, std:: list < T, A > 는 내부 타입 Node<T> 의 노드를 할당자 A::rebind<Node<T>>::other (C++11 이전) std:: allocator_traits < A > :: rebind_alloc < Node < T >> 를 사용하여 할당하며, 이는 A::rebind<Node<T>>::other 를 기반으로 구현됩니다 (A가 std::allocator 인 경우). (C++11 이후) .

멤버 타입 is_always_equal LWG 이슈 3170 을 통해 사용 중단되었습니다. 이는 기본적으로 std::allocator 에서 파생된 사용자 정의 할당자를 항상 동등한 것으로 취급하기 때문입니다. std:: allocator_traits < std :: allocator < T >> :: is_always_equal 는 사용 중단되지 않았으며, 그 멤버 상수 value 는 모든 T 에 대해 true 입니다.

예제

#include <iostream>
#include <memory>
#include <string>
int main()
{
    // int형을 위한 기본 할당자
    std::allocator<int> alloc1;
    // 직접 사용 가능한 멤버들 시연
    static_assert(std::is_same_v<int, decltype(alloc1)::value_type>);
    int* p1 = alloc1.allocate(1); // 하나의 int를 위한 공간
    alloc1.deallocate(p1, 1);     // 그리고 해제됨
    // 하지만 이들도 traits를 통해 사용 가능하므로 필요 없음
    using traits_t1 = std::allocator_traits<decltype(alloc1)>; // 해당 trait
    p1 = traits_t1::allocate(alloc1, 1);
    traits_t1::construct(alloc1, p1, 7);  // int 생성
    std::cout << *p1 << '\n';
    traits_t1::deallocate(alloc1, p1, 1); // 하나의 int를 위한 공간 해제
    // string을 위한 기본 할당자
    std::allocator<std::string> alloc2;
    // 해당 traits
    using traits_t2 = std::allocator_traits<decltype(alloc2)>;
    // trait를 사용하여 할당자 재바인딩하면 동일한 타입을 얻음
    traits_t2::rebind_alloc<std::string> alloc_ = alloc2;
    std::string* p2 = traits_t2::allocate(alloc2, 2); // 2개의 string을 위한 공간
    traits_t2::construct(alloc2, p2, "foo");
    traits_t2::construct(alloc2, p2 + 1, "bar");
    std::cout << p2[0] << ' ' << p2[1] << '\n';
    traits_t2::destroy(alloc2, p2 + 1);
    traits_t2::destroy(alloc2, p2);
    traits_t2::deallocate(alloc2, p2, 2);
}

출력:

7
foo bar

결함 보고서

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

DR 적용 대상 게시된 동작 올바른 동작
LWG 2103 C++11 allocator 간의 불필요한 비교가 필요할 수 있었음 propagate_on_container_move_assignment 제공됨
LWG 2108 C++11 allocator 가 상태를 가지지 않음을 나타낼 방법이 없었음 is_always_equal 제공됨

참고 항목

할당자 타입에 대한 정보를 제공함
(클래스 템플릿)
다중 수준 컨테이너를 위한 다중 수준 할당자를 구현함
(클래스 템플릿)
지정된 타입이 uses-allocator 생성 방식을 지원하는지 확인함
(클래스 템플릿)