我有一些困惑。假设我有一个任意的C++分配器 - 就像这样:
template<class T>
struct my_allocator
{
template<class Other>
struct rebind { typedef my_allocator<Other> other; };
// [other members here]
};
现在考虑以下代码(请阅读注释):
typedef my_allocator<int> Alloc;
Alloc alloc = get_my_allocator(); // assume this works properly
long *const p = Alloc::rebind<long>::other(alloc).allocate(1, NULL);
// Notice that the rebound allocator for 'long' is now destroyed
// Can a NEW rebound allocator for 'long' deallocate the memory from the old one?
Alloc::rebind<long>::other(alloc).deallocate(p, 1);
// i.e., does the 'int' allocator 'alloc' keep alive the 'long' memory pool too?
在哪个时刻可以释放后备存储池?
或者换句话说:哪个分配器共享哪个内存池的所有权?
我一直认为同一值类型的分配器共享其自己的内存池,但现在我想到它们还可能共享所有重新绑定分配器背后的内存池,即使它们管理完全不同类型。
必须让重新绑定类型的分配器“保持活动”,直到它们全部被销毁吗?
如果 C++03 和 C++11 的答案不同,请解释两者之间的区别。