自定义分配器无法重新绑定到其他类型

时间:2011-09-30 09:20:23

标签: c++ templates stl allocator

所有

我的自定义分配器的代码是为了成为其他分配器的代理而编写的,以便能够收集分配统计信息或其他任何内容

 template<int Id, class T, class BaseAlloc> 
class SelfTracingAllocator
    : public BaseAlloc
{
typedef AllocationStatistics<Id> StatsType;

public:
    typedef typename BaseAlloc              base;
    typedef typename base::raw_pointer      raw_pointer;
    typedef typename base::pointer          pointer;
    typedef typename base::const_pointer    const_pointer;
    typedef typename base::reference        reference;
    typedef typename base::const_reference  const_reference;
    typedef typename base::size_type        size_type;
    typedef typename base::value_type       value_type;
    typedef typename base::difference_type  difference_type;

    template<typename Other>
    struct rebind
    {
        typedef SelfTracingAllocator<Id, Other, base> other;
    };

    SelfTracingAllocator()
    {
    }

    SelfTracingAllocator( const SelfTracingAllocator& rhs )
    {
    }

    template<typename Other>
    inline SelfTracingAllocator( const SelfTracingAllocator<Id, Other, base>& rhs )
    {
    }

    ~SelfTracingAllocator()
    {
    }

    template<typename Other>
    inline SelfTracingAllocator& operator=( const SelfTracingAllocator<Id, Other, base>& rhs )
    {
        return (*this);
    }

    inline raw_pointer allocate(size_type count, const void* ptr)
    {
        StatsType::allocated_ += count * sizeof(value_type);
        return base::allocate(count, ptr);
    }

    inline raw_pointer allocate(size_type count)
    {
        StatsType::allocated_ += count * sizeof(value_type);
        return base::allocate(count);
    }

    inline void deallocate( pointer ptr, size_type count )
    {
       StatsType::deallocated_ += count * sizeof(value_type);
       return base::deallocate(ptr, count);
    }

};

template<class T, class U, int Id, class BaseAlloc>
inline bool operator==(const SelfTracingAllocator<Id, T, BaseAlloc>& lhs, const SelfTracingAllocator<Id, U, BaseAlloc>& rhs)
{
    return true;
}

template<class T, class U, int Id, class BaseAlloc>
inline bool operator!=(const SelfTracingAllocator<Id, T, BaseAlloc>& lhs, const SelfTracingAllocator<Id, U, BaseAlloc>& rhs )
{
    return false;
}


template<class T, int Id>
struct my_allocator
{
    typedef SelfTracingAllocator<Id, T, some_allocator<T, MemMgr> > type;
};

当我使用下面的分配器时

typedef custom_string<wchar_t, char_traits<wchar_t>, my_allocator<int, 0>::type > string_t;

我收到如下错误

error C2664: 'char_traits<T>::move' : cannot convert parameter 1 from 'int *' to 'wchar_t *'
1>        with
1>        [
1>            T=wchar_t
1>        ]
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>        D:\VRS_Branch\detail/dynamic_buffer.hpp(95) : while compiling class template member function 'void detail::dynamic_buffer<T,Alloc,Traits>::reserve(unsigned int)'
1>        with
1>        [
1>            T=wchar_t,
1>            Alloc=SelfTracingAllocator<0,int,some_allocator<int,MemMgr>>,
1>            Traits=char_traits<wchar_t>
1>        ]

为什么会这样?我有重新绑定结构和重新绑定复制构造函数,所以出了什么问题?

提前致谢, 安德烈

1 个答案:

答案 0 :(得分:2)

使用

怎么样?
template<typename Other>
struct rebind
{
    typedef SelfTracingAllocator<
        Id
        , Other
        , typename base::template rebind<Other>::other
    > other;
};

现在,如果您有SelfTracingAllocator<0, int, std::allocator<int> >,那么重新绑定到wchar_t将会产生SelfTracingAllocator<0, wchar_t, std::allocator<int> >。这似乎不对。