• Advertisement
Sign in to follow this  

Custom Allocator

This topic is 2451 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I have written a little custom allocator for std::map:

template<typename T>
class MemManagerMapAllocator {
public :

typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

public :
template<typename U>
struct rebind {
typedef MemManagerMapAllocator<U> other;
};

public :
inline explicit MemManagerMapAllocator() {}
inline ~MemManagerMapAllocator() {}
inline explicit MemManagerMapAllocator(MemManagerMapAllocator const&) {}
template<typename U>
inline explicit MemManagerMapAllocator(MemManagerMapAllocator<U> const&) {}


inline pointer address(reference r) { return &r; }
inline const_pointer address(const_reference r) { return &r; }


inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0) {
return reinterpret_cast<pointer>(malloc(cnt * sizeof(T)));
}

inline void deallocate(pointer p, size_type) {
free(p);
}

inline size_type max_size() const {
return std::numeric_limits<size_type>::max() / sizeof(T);
}

inline void construct(pointer p, const T& t) { new(p) T(t); }
inline void destroy(pointer p) { p->~T(); }

inline bool operator==(MemManagerMapAllocator const&) { return true; }
inline bool operator!=(MemManagerMapAllocator const& a) { return !operator==(a); }
}; //


But when I try to declare a private member variable of a map with my allocator:
typedef MemManagerMapAllocator< std::pair<const void*, AllocInfo> > MapAllocator;
std::map<void*, AllocInfo, std::less<void*>, MapAllocator> mAllocMap;

I get this cryptic error:
>D:\Microsoft Visual Studio 10.0\VC\include\xtree(698): error C2664: 'std::_Tree_val<_Traits>::_Tree_val(const std::less<_Ty> &,ple::MemManagerMapAllocator<T>)' : cannot convert parameter 2 from 'const ple::MemManagerMapAllocator<T>' to 'ple::MemManagerMapAllocator<T>'
1> with
1> [
1> _Traits=std::_Tmap_traits<void *,ple::MemManager::AllocInfo,std::less<void *>,ple::MemManager::MapAllocator,false>,
1> _Ty=void *,
1> T=std::pair<void *const ,ple::MemManager::AllocInfo>
1> ]
1> and
1> [
1> T=std::pair<void *const ,ple::MemManager::AllocInfo>
1> ]
1> and
1> [
1> T=std::pair<void *const ,ple::MemManager::AllocInfo>
1> ]
1> Constructor for class 'ple::MemManagerMapAllocator<T>' is declared 'explicit'
1> with
1> [
1> T=std::pair<void *const ,ple::MemManager::AllocInfo>
1> ]
1> D:\Microsoft Visual Studio 10.0\VC\include\xtree(695) : while compiling class template member function 'std::_Tree<_Traits>::_Tree(const std::less<_Ty> &,const ple::MemManagerMapAllocator<T> &)'
1> with
1> [
1> _Traits=std::_Tmap_traits<void *,ple::MemManager::AllocInfo,std::less<void *>,ple::MemManager::MapAllocator,false>,
1> _Ty=void *,
1> T=std::pair<void *const ,ple::MemManager::AllocInfo>
1> ]
1> D:\Microsoft Visual Studio 10.0\VC\include\map(81) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<void *,ple::MemManager::AllocInfo,std::less<void *>,ple::MemManager::MapAllocator,false>
1> ]
1> E:\Programmieren\C++\MemManager.h(185) : see reference to class template instantiation 'std::map<_Kty,_Ty,_Pr,_Alloc>' being compiled
1> with
1> [
1> _Kty=void *,
1> _Ty=ple::MemManager::AllocInfo,
1> _Pr=std::less<void *>,
1> _Alloc=ple::MemManager::MapAllocator
1> ][/quote]
Does anyone have an idea what could be the problem?

Share this post


Link to post
Share on other sites
Advertisement
You could try taking out all those unnecessary 'explicit' keywords in constructors that do not need it. That would be all of them.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement