c++ - 自定义分配器无法重新绑定(bind)到其他类型

标签 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>        ]

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

提前致谢, 安德烈

最佳答案

使用怎么样

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

?

现在如果你有一个 SelfTracingAllocator<0, int, std::allocator<int> >然后重新绑定(bind)到 wchar_t将产生 SelfTracingAllocator<0, wchar_t, std::allocator<int> > .这似乎不对。

关于c++ - 自定义分配器无法重新绑定(bind)到其他类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7608342/

相关文章:

c++ - STL 列表性能很差

c++ - Visual Studio - 启用优化导致崩溃

c++ - 如何在项目中实现预编译头文件

c++ - boost::shared_ptr,ctor 中的一个原子递增,但 dtor 中的两个原子递减?

c++ - boost 如何将类的类型列表实现为 "options"?

c++ - VS 2012 模板错误

c++ - 迭代器失效规则

c++ - 如何防止我的函数在 RemoveDirectory() WINAPI 中延迟删除?

c++ - 删除 std::set 的最终成员

c++ - 在 cuda/c++ 代码中使用模板类