gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:32:31 25 4
gpt4 key购买 nike

全部

我有我的自定义分配器的代码,它是为了成为其他分配器的代理而编写的,例如能够收集分配统计信息或其他任何东西

 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com