【发布时间】:2013-02-01 07:36:48
【问题描述】:
我制作了一个自定义的slab分配器,它使用mmap来分配固定大小的段池。这些段在逻辑上是连续的,但在物理上是离散的。 我还定义了一个指针包装类,它包含从池的逻辑起点开始的偏移量。 指针类看起来像:
template<typename T>
struct offptr_t {
typedef offptr_t<T> this_t;
typedef T element_type;
typedef OFFSET difference_type;
template<typename U> struct rebind { typedef offptr_t<U> other; };
offptr_t(const mem_pool *p, OFFSET o)
: pool(p), offset(o)
{}
// ...
};
这是分配器:
template<typename T>
struct mem_pool_allocator {
public:
typedef mem_pool_allocator<T> this_t;
typedef T value_type;
typedef offptr_t<T> pointer;
typedef const offptr_t<T> const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef int64_t difference_type;
template< class U > struct rebind { typedef mem_pool_allocator<U> other; };
// ...
};
然后我根据 STL 要求定义了 pointer_traits 和 iterator_traits 类:
namespace std {
template<typename T>
struct pointer_traits<offptr_t<T>> {
typedef typename offptr_t<T> pointer;
template<typename U> struct rebind { typedef offptr_t<U> other; };
};
template<typename T>
struct iterator_traits<offptr_t<T>> {
typedef typename offptr_t<T> pointer;
typedef typename pointer::difference_type difference_type;
typedef typename pointer::element_type value_type;
typedef typename pointer::element_type &reference;
typedef std::random_access_iterator_tag iterator_category;
};
} // End of namespace std
当我在 libc++ 中将这些类与 STL 容器一起使用时,在 c++/v1/vector 中出现了几个编译错误:
template <class _Tp, class _Allocator>
_LIBCPP_INLINE_VISIBILITY inline
void
__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, false_type) _NOEXCEPT
{
while (__new_last != __end_)
__alloc_traits::destroy(__alloc(), const_cast<pointer>(--__end_));
}
template <class _Tp, class _Allocator>
_LIBCPP_INLINE_VISIBILITY inline
void
__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, true_type) _NOEXCEPT
{
__end_ = const_cast<pointer>(__new_last);
}
Vector 在指针类型上使用 const_cast,const_cast 只能与原始指针/引用一起使用,并且不能重载,这意味着它无法与自定义的类指针对象一起使用。
我做错了什么还是只是 libc++ 中 STL 实现的一个缺陷?
【问题讨论】:
标签: pointers stl containers allocator libc++