what does (template) rebindlt;gt; do?(什么(模板)重新绑定lt;gt;做?)
问题描述
试图了解更多关于标准库是如何实现的,我正在检查 Visual Studio 中的所有容器.在这里我看到了一些奇怪的结构:
trying to learn more about how the standard library is actually implemented I'm inspecting all containers in visual studio.. Here I see some curious structure:
在std::list<>的某个基类中找到以下typedef
In some base class of a std::list<> The following typedef is found
typedef typename _Alloc::template rebind<_Ty>::other _Alty;
其中_Alloc"对应于分配器模板参数(和 _Ty 包含的类型).我很难找到对这个关键字"的一个很好的解释.到目前为止我发现的最好的事情是它是分配器接口的一部分.尽管即使 cppreference 也不能很好地解释这一点.
Where "_Alloc" corresponds with the allocator template argument (and _Ty the contained type). I have trouble finding a good explanation of this "keyword". Best thing I've found so far is that it is part of the allocator interface. Though even cppreference isn't very good in explaining this.
这个模板重新绑定<>有什么作用?为什么需要在那个位置?
What does this template rebind<> do? And why is it necessary at that location?
推荐答案
_Alloc 模板用于获取某种类型的对象.容器可能需要分配不同类型的对象.例如,当您有一个 std::list 时,分配器 A 旨在分配 T 类型的对象,但是std::list 实际上需要分配一些节点类型的对象.调用节点类型 _Ty,std::list 需要为正在使用的 _Ty 对象获取分配器A 提供的分配机制.使用
The _Alloc template is used to obtain objects of some type. The container may have an internal need to allocate objects of a different type. For example, when you have a std::list<T, A>, the allocator A is meant to allocate objects of type T but the std::list<T, A> actually needs to allocate objects of some node type. Calling the node type _Ty, the std::list<T, A> needs to get hold of an allocator for _Ty objects which is using the allocation mechanism provided by A. Using
typename _A::template rebind<_Ty>::other
指定对应的类型.现在,此声明中有一些语法上的烦恼:
specifies the corresponding type. Now, there are a few syntactic annoyances in this declaration:
- 由于
rebind是_A的成员模板,_A是模板参数,所以rebind成为一个依赖名称.要表明依赖名称是模板,它需要以template为前缀.如果没有template关键字,<将被视为小于运算符. - 名称
other也依赖于模板参数,即它也是一个依赖名称.要指示从属名称是一种类型,需要typename关键字.
- Since
rebindis a member template of_Aand_Ais a template argument, therebindbecomes a dependent name. To indicate that a dependent name is a template, it needs to be prefixed bytemplate. Without thetemplatekeyword the<would be considered to be the less-than operator. - The name
otheralso depends on a template argument, i.e., it is also a dependent name. To indicate that a dependent name is a type, thetypenamekeyword is needed.
这篇关于什么(模板)重新绑定<>做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么(模板)重新绑定<>做?
基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
