std::pointer_traits

定义于头文件 <memory>
template< class Ptr > struct pointer_traits;
(1) (C++11 起)
template< class T > struct pointer_traits<T*>;
(2) (C++11 起)

pointer_traits 类模板提供标准化方法,访问类指针类型(缀饰指针,如 boost::interprocess::offset_ptr )的某些属性。标准模板 std::allocator_traits 依靠 pointer_traits 确定多种分配器 (Allocator) 所要求的 typedef 的默认行为。

1) 非特化 pointer_traits 声明下列类型:

目录

成员类型

类型 定义
pointer Ptr
element_type 若存在则为 Ptr::element_type 。否则若 Ptr 是模板实例化 Template<T, Args...> 则为 T
difference_type 若存在则为 Ptr::difference_type ,否则为 std::ptrdiff_t

成员别名模板

模板 定义
template <class U> using rebind 若存在则为 Ptr::rebind<U> ,否则若 Ptr 是模板实例化 Template<T, Args...> 则为 Template<U, Args...>

成员函数

[静态]
获得指向其参数的可解引用指针
(公开静态成员函数)

2) 为指针类型 T* 提供的特化,它声明下列类型

成员类型

类型 定义
pointer T*
element_type T
difference_type std::ptrdiff_t

成员别名模版

模板 定义
template< class U > using rebind U*

成员函数

[静态]
获得指向其参数的可解引用指针
(公开静态成员函数)

3) 对用户定义的缀饰指针类型的特化可定义额外的静态成员函数

可选成员函数

[静态] (C++20)
从缀饰指针获得裸指针( pointer_to 的反函数)
(公开静态成员函数)

注意

重绑定成员模板别名,使得可以由指向 T 的类指针类型,获取指向 U 的相同类指针类型。例如,

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

示例

#include <memory>
#include <iostream>
 
template <class Ptr>
struct BlockList
{
   // 预定义内存块
   struct block;
 
   // 从指针类别 Ptr 定义指向内存块的指针
   // 若 Ptr 是任何 T* 类型,则 block_ptr_t 为 block*
   // 若 Ptr 是 smart_ptr<T> ,则 block_ptr_t 为 smart_ptr<block>
   typedef typename std::pointer_traits<Ptr>::template rebind<block> block_ptr_t;
 
   struct block
   {
      std::size_t size;
      block_ptr_t next_block;
   }; 
 
   block_ptr_t free_blocks;
}; 
 
int main()
{
    BlockList<int*> bl1;
    // bl1.free_blocks 的类型是 block*
 
    BlockList<std::shared_ptr<char>> bl2;
    // bl2.free_blocks 的类型是 std::shared_ptr<block>
    std::cout << bl2.free_blocks.use_count() << '\n';
}

输出:

​0​

参阅

提供关于分配器类型的信息
(类模板)
(C++11)
获得对象的实际地址,即使重载了 & 运算符
(函数模板)

版本历史

  • (当前 | 先前 2017年11月14日 (二) 08:14Fruderica讨论 | 贡献. . (3,614字节) (+218). . (撤销)
  • 当前 | 先前 2017年5月29日 (一) 01:23Fruderica讨论 | 贡献 . . (3,396字节) (-3). . (撤销)
  • 当前 | 先前 2017年5月9日 (二) 09:36Fruderica讨论 | 贡献. . (3,399字节) (-1,411). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 17:56P12bot讨论 | 贡献 . . (4,810字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:05P12bot讨论 | 贡献 . . (4,810字节) (-275). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 15:46P12bot讨论 | 贡献 . . (5,085字节) (+265). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 03:39P12讨论 | 贡献 . . (4,820字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (4,820字节) (-139). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:10P12讨论 | 贡献 . . (4,959字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (4,959字节) (+4,959). . (Translated from the English version using Google Translate)