Namespaces
Variants

std:: pointer_traits

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)
std::pointer_traits
Member functions
(C++20) (optional)
헤더 파일에 정의됨 <memory>
template < class Ptr >
struct pointer_traits ;
(1) (C++11부터)
template < class T >
struct pointer_traits < T * > ;
(2) (C++11부터)

pointer_traits 클래스 템플릿은 포인터와 유사한 타입들( fancy pointers , 예를 들어 boost::interprocess::offset_ptr )의 특정 속성에 접근하기 위한 표준화된 방법을 제공합니다. 표준 템플릿 std::allocator_traits Allocator 에서 요구하는 다양한 typedef들의 기본값을 결정하기 위해 pointer_traits 에 의존합니다.

1) 비특수화된 pointer_traits 는 조건부로 다음 멤버들을 선언합니다:

/*element-type-of*/ < Ptr > 를 다음과 같이 정의합니다:

  • Ptr :: element_type 이 존재하는 경우;
  • 그렇지 않고, Ptr 이 클래스 템플릿 특수화 Template < T, Args... > 인 경우 T (여기서 Args... 는 0개 이상의 타입 인자들);
  • 그 외의 경우, 정의되지 않음.

만약 /*element-type-of*/ < Ptr > 가 정의되지 않았다면, 기본 템플릿은 이 페이지에서 명시된 멤버들을 갖지 않습니다.

목차

멤버 타입

타입 정의
pointer Ptr
element_type /*element-type-of*/ < Ptr >
difference_type Ptr :: difference_type 이 존재하는 경우, 그렇지 않으면 std::ptrdiff_t

멤버 별칭 템플릿

템플릿 정의
template < class U > using rebind Ptr :: rebind < U > 이 존재하는 경우, 그렇지 않으면 Template < U, Args... > (만약 Ptr 가 템플릿 특수화 Template < T, Args... > 인 경우)

멤버 함수

[static]
인자에 대한 역참조 가능한 포인터를 얻음
(public static member function)
2) 포인터 타입에 대한 특수화가 제공되며, T * , 이는 다음 멤버들을 선언합니다:

멤버 타입

타입 정의
pointer T *
element_type T
difference_type std::ptrdiff_t

멤버 앨리어스 템플릿

템플릿 정의
template < class U > using rebind U *

멤버 함수

[static]
인수에 대한 역참조 가능한 포인터를 얻음
(public static member function)

프로그램 정의 특수화의 선택적 멤버 함수

[static] (C++20) (optional)
팬시 포인터(fancy pointer)로부터 원시 포인터(raw pointer)를 획득합니다 ( pointer_to 의 역함수)
(public static member function)

참고 사항

rebind 멤버 템플릿 별칭은 T 를 가리키는 포인터와 유사한 타입이 주어졌을 때, 동일한 포인터와 유사한 타입으로 U 를 가리키는 타입을 얻을 수 있게 합니다. 예를 들어,

using another_pointer = std::pointer_traits<std::shared_ptr<int>>::rebind<double>;
static_assert(std::is_same<another_pointer, std::shared_ptr<double>>::value);

사용자 정의 고급 포인터 타입에 대한 특수화는 std::to_address 의 동작을 사용자 정의하기 위해 추가적인 정적 멤버 함수 to_address 를 제공할 수 있습니다.

(since C++20)
기능 테스트 매크로 표준 기능
__cpp_lib_constexpr_memory 201811L (C++20) constexpr in std::pointer_traits

예제

#include <iostream>
#include <memory>
template<class Ptr>
struct BlockList
{
    // 메모리 블록을 미리 정의
    struct block;
    // 포인터 Ptr의 종류로부터 메모리 블록에 대한 포인터 정의
    // Ptr이 어떤 종류의 T*라면, block_ptr_t는 block*
    // Ptr이 smart_ptr<T>라면, block_ptr_t는 smart_ptr<block>
    using block_ptr_t = typename std::pointer_traits<Ptr>::template rebind<block>;
    struct block
    {
        std::size_t size{};
        block_ptr_t next_block{};
    };
    block_ptr_t free_blocks;
};
int main()
{
    [[maybe_unused]]
    BlockList<int*> bl1;
    // bl1.free_blocks의 타입은 BlockList<int*>::block*
    BlockList<std::shared_ptr<char>> bl2;
    // bl2.free_blocks의 타입은
    // std::shared_ptr<BlockList<std::shared_ptr<char>>::block>
    std::cout << bl2.free_blocks.use_count() << '\n';
}

출력:

​0​

결함 보고서

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

DR 적용 대상 게시된 동작 올바른 동작
LWG 3545 C++11 기본 템플릿에서 element_type 이 유효하지 않을 때 하드 에러 발생 SFINAE 친화적으로 변경됨

참고 항목

할당자 타입에 대한 정보를 제공함
(클래스 템플릿)
(C++11)
& 연산자가 오버로드된 경우에도
객체의 실제 주소를 획득함
(함수 템플릿)