什么是(template)rebind<>做? [英] what does (template) rebind<> do?

查看:618
本文介绍了什么是(template)rebind<>做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图了解标准库如何实际实现我正在检查visual studio中的所有容器。这里我看到一些好奇的结构:



在一些 std :: list的基类发现以下typedef

  typedef typename _Alloc :: template rebind< _Ty> :: other _Alty; 

其中_Alloc与分配器模板参数(以及_包含的类型)相对应。我找不到这个关键字的很好的解释。最好的thign我发现到目前为止,它是分配器接口的一部分。虽然即使 cppreference 在解释此问题上也不是很好。



这个模板rebind<> 是什么?

解决方案

_Alloc 模板用于获得某种类型的对象。容器可以具有分配不同类型的对象的内部需要。例如,当你有 std :: list< T,A> 时,分配器 A T 的对象,但是 std :: list 实际上需要分配某个节点类型的对象。调用节点类型 _Ty std :: list< T,A> 需要获取分配器 _Ty 使用由 A 提供的分配机制的对象。使用

  typename _A :: template rebind< _Ty> :: other 
pre>

指定相应的类型。现在,在此声明中有一些语法烦恼:


  1. 由于 rebind _A _A 的成员模板是模板参数, rebind 成为依赖名称。要指示依赖名称是模板,需要使用 template 作为前缀。如果没有模板关键字,则< 将被视为小于运算符。

  2. 名称其他也取决于模板参数,即它也是一个依赖名称。要指示依赖名称是一个类型,需要 typename 关键字。


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:

In some base class of a std::list<> The following typedef is found

typedef typename _Alloc::template rebind<_Ty>::other _Alty;

Where "_Alloc" corresponds with the allocator template argument (and _Ty the contained type). I have trouble finding a good explanation of this "keyword". Best thign 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?

解决方案

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:

  1. Since rebind is a member template of _A and _A is a template argument, the rebind becomes a dependent name. To indicate that a dependent name is a template, it needs to be prefixed by template. Without the template keyword the < would be considered to be the less-than operator.
  2. The name other also depends on a template argument, i.e., it is also a dependent name. To indicate that a dependent name is a type, the typename keyword is needed.

这篇关于什么是(template)rebind&lt;&gt;做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆