对标准库分配器指针类型的要求 [英] Requirements on standard library allocator pointer types
问题描述
我想写一个四叉树稀疏矩阵类。简而言之, quadtree_matrix
是零矩阵或四元(ne,nw,se,sw)
quadtree_matrix< T>
。
I am trying to write a quadtree sparse matrix class. In short, a quadtree_matrix<T>
is either the zero matrix or a quadruple (ne, nw, se, sw)
of quadtree_matrix<T>
.
我想最终测试不同的分配方案,因为这可能会影响线性代数运算的性能。因此,我还将在标准分配器类型上使用模板 quadtree_matrix
,以便我可以重用现有的分配器。
I'd like eventually to test different allocation schemes since this will probably impact the performance of linear algebra operations. So I will also template quadtree_matrix
on a standard allocator type, so that I can reuse existing allocators.
必须分配两种不同类型的数据: T
或节点
,其中包含四个指针T或节点)。对于所有的算法,我会考虑,我知道确切的是什么样的数据,因为我知道什么是我在算法的任何点(我甚至不需要存储这些大小)的子矩阵的大小。
I will have to allocate two different kind of data: either a T
, or a node
, which contains four pointers (to either T or node). For all the algorithms I will consider, I know for sure what kind of data to expect because I know what are the sizes of the submatrices I am facing at any point of the algorithm (I don't even need to store these sizes).
我当然会使用两个不同的分配器:这是确定,因为分配器类型提供 rebind
模板,一个模板复制构造函数(并且意图用作值类型,因为 get_allocator
标准容器的成员建议通过返回一个副本)。
I will of course be using two different allocators: this is ok, since allocator types provide the rebind
template and a template copy constructor (and are intended to be used as value types, as the get_allocator
members of standard containers suggest by returning a copy).
问题是allocator的成员函数使用一个指针
类型,这不是一个必须的指针。一些分配器(boost进程间分配器)广泛地使用此功能。
The problem is that allocator member functions use a certain pointer
type, which is not required to be a vanilla pointer. Some allocators (boost interprocess allocators) use this feature extensively.
如果分配器指针类型是花园种类指针,我会没有问题:至少,我可以使用指针void和reinterpret_cast他们正确的类型 node *
或 T *
)。我也可以使用一个联合(可能更好)。
If the allocator pointer types were garden variety pointers, I would have no problems: at the very least, I could use pointers to void and reinterpret_cast them to the right type (either node*
or T*
). I could also use a union (probably better).
据我所知,没有要求PODness的 allocator ::指针
类型。他们只需要是随机访问迭代器。
As far as I know, there is no requirement on the PODness of the allocator::pointer
types. They are only required to be random access iterators.
现在,我的问题是:
分配器类模板 A
(或其等效 A :: rebind< T>其他
是否有任何保证:
Given an allocator class template A<T>
(or its equivalent A::rebind<T>::other
), is there any guarantee on:
- 静态强制转换的能力
A< T& :: pointer
toA< U> :: pointer
提供U
T ?
- 静态强制转换的能力
A< T& :指向
到
A< U> ::指针
提供T
U
并且castee的运行时类型(在此上下文中的任何内容)是U
? - 类型
A< void> :: pointer
(如果有意义)? li>
- The ability to static cast
A<T>::pointer
toA<U>::pointer
providedU
is an accessible base ofT
? - The ability to static cast
A<T>::pointer
toA<U>::pointer
providedT
is an accessible base ofU
and the "runtime type" (whatever this means in this context) of the castee isU
? - The type
A<void>::pointer
(if this makes sense) ?
还是有我的问题的解决方案我没有想过?
Or is there a solution to my problem I didn't think about ?
推荐答案
从20.1.5 / 2中的表中可以清楚地看出, A< T> :: pointer
的类型必须是指向 T
。由于这些指针类型通常可以转换你的1和2是真的。然后, A< void> :: pointer
必须是 void *
。
From the tables in 20.1.5/2 it clearly indicates that the type of A<T>::pointer
must be "pointer to T
". Since those pointer types are normally convertible your 1 and 2 are true. It follows then that A<void>::pointer
must be void*
.
编辑:
在20.1.5 / 4中也有明确的措辞(它适用于什么标准容器可能假设的分配器):
There's also explicit wording in 20.1.5/4 (it applies to what standard containers may assume about allocators):
typedef成员指针
const_pointer,size_type和
difference_type需要为
T *,T const *,size_t和ptrdiff_t,
。
The typedef members pointer, const_pointer, size_type, and difference_type are required to be T*,T const*, size_t, and ptrdiff_t, respectively.
这篇关于对标准库分配器指针类型的要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!