std::pointer_traits
来自cppreference.com
|
|
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
| 在头文件 <memory> 中定义
|
||
| template< class Ptr > struct pointer_traits; |
(1) | (C++11 起) |
| template< class T > struct pointer_traits<T*>; |
(2) | (C++11 起) |
pointer_traits类模板提供了标准化的方式来访问指针类类型的某些属性。 std::allocator_traits依赖于pointer_traits标准模板,以确定不同的类型定义的默认值所需要Allocator.原文:
The
pointer_traits class template provides the standardized way to access certain properties of pointer-like types. The standard template std::allocator_traits relies on pointer_traits to determine the defaults for various typedefs requires by Allocator.非专门
pointer_traits声明以下几种类型:原文:
The non-specialized
pointer_traits declares the following types:目录 |
[编辑] 会员类型
| 类型
|
Definition |
pointer
|
Ptr |
element_type
|
Ptr::element_type如果存在的话。否则
T如果Ptr是一个模板实例化Template<T, Args...> 原文: Ptr::element_type if present. Otherwise T if Ptr is a template instantiation Template<T, Args...> |
difference_type
|
Ptr::difference_type(如果存在),否则std::ptrdiff_t
原文: Ptr::difference_type if present, otherwise std::ptrdiff_t |
[编辑] 会员别名模板
| 模板
|
Definition |
| template <class U> using rebind | Ptr::rebind<U>如果存在,否则Tempate<U, Args...>如果
Ptr是一个模板实例化Template<T, Args...> 原文: Ptr::rebind<U> if exists, otherwise Tempate<U, Args...> if Ptr is a template instantiation Template<T, Args...> |
[编辑] 成员函数
| [静态的]</div></div>
|
获得dereferencable它的参数的指针 原文: obtains a dereferencable pointer to its argument (公共静态成员函数) |
提供一个专业化的指针类型,
T*,声明如下类型原文:
A specialization is provided for pointer types,
T*, which declares the following types[编辑] 会员类型
| 类型
|
Definition |
pointer
|
T* |
element_type
|
T |
difference_type
|
std::ptrdiff_t |
[编辑] 会员别名模板
| 模板
|
Definition |
| template< class U > using rebind | U* |
[编辑] 成员函数
| [静态的]</div></div>
|
获得dereferencable它的参数的指针 原文: obtains a dereferencable pointer to its argument} (公共静态成员函数) |
[编辑] 注释
在重新绑定构件模板别名使得有可能,给出一个指针样型点为T,以获得相同的指针类型指向U.例如,
原文:
The rebind member template alias makes it possible, given a pointer-like type that points to T, to obtain the same pointer-like type that points to U. For example,
的std :: is_same性病:: pointer_traits <性病:: shared_ptr的<int>的>重新绑定<double>的的std ::的unique_ptr <double>的>值),“”)
原文:
std::is_same<std::pointer_traits< std::shared_ptr<int>>::rebind<double>, std::unique_ptr<double> >::value), "")
[编辑] 示例
#include <memory> #include <iostream> template <class Ptr> struct BlockList { // Predefine a memory block struct block; // Define a pointer to a memory block from the kind of pointer Ptr s // If Ptr is any kind of T*, block_ptr_t is block* // If Ptr is smart_ptr<T>, block_ptr_t is 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; // The type of bl1.free_blocks is block* BlockList<std::shared_ptr<char>> bl2; // The type of bl2.free_blocks is std::shared_ptr<block> std::cout << bl2.free_blocks.use_count() << '\n'; }
输出:
0
[编辑] 另请参阅
| (C++11) |
提供分配器类型的信息 原文: provides information about allocator types (类模板) |
| (C++11) |
获得的对象的实际地址,即使在“&”运算符被重载 原文: obtains actual address of an object, even if the & operator is overloaded (函数模板) |