emhash icon indicating copy to clipboard operation
emhash copied to clipboard

Compile error when using custom Allocator.

Open xmmmmmovo opened this issue 1 year ago • 2 comments

When I define a CustomAllocator and use it in EmHashMap, a compile error occurs:

[build] \emhash\hash_table8.hpp(722): error C2440: 'return': cannot convert from 'initializer list' to 'std::pair<emhash8::HashMap<int,int,std::hash<int>,std::equal_to<int>,TestAllocator<int>,emhash8::DefaultPolicy>::iterator,bool>'
[build] \emhash\hash_table8.hpp(722): note: 'std::pair<emhash8::HashMap<int,int,std::hash<int>,std::equal_to<int>,TestAllocator<int>,emhash8::DefaultPolicy>::iterator,bool>::pair': function does not take 2 arguments
[build] \MSVC\14.42.34433\include\utility(326): note: could be 'std::pair<emhash8::HashMap<int,int,std::hash<int>,std::equal_to<int>,TestAllocator<int>,emhash8::DefaultPolicy>::iterator,bool>::pair(std::piecewise_construct_t,std::tuple<_Types1...>,std::tuple<_Types2...>)'
......

And I find a reason is that htype is not implement template fully, a solution is add all template like this commit 1a76d71a02c6c1b8903c315b144109b97dedf1c4 So, if this repo is pr welcomed, I'm glad to have a contribution, thanks.

...
public:
    using htype = HashMap<KeyT, ValueT, HashT, EqT>; // no allocator and policy.
    using value_type = std::pair<KeyT, ValueT>; //TODO set to const KeyT
    using key_type = const KeyT;
    using mapped_type = ValueT;
    //using dPolicy = Policy;
...

Demo code:

#include "hash_table8.hpp"

template <class T> class TestAllocator : public std::allocator<T> {
  using std::allocator<T>::allocator;

  T *allocate(size_t n) {
    printf("allocate %zu\n", n);
    return std::allocator<T>::allocate(n);
  }

  void deallocate(T *p, size_t n) {
    printf("deallocate %zu\n", n);
    std::allocator<T>::deallocate(p, n);
  }
};

template <class T> using HashTable = emhash8::HashMap<T, T, std::hash<T>, std::equal_to<T>, TestAllocator<T>>;

int main() { 
    HashTable<int> ht;
    ht.reserve(1000000);
    ht.insert(std::make_pair(1, 1));
    return 0;
}

xmmmmmovo avatar Aug 16 '25 13:08 xmmmmmovo

If you'd like, you can try submitting a file pull request to implement the Allocator. I have no plan to implement Allocator which is used only for compatibility purposes. using htype = HashMap<KeyT, ValueT, HashT, EqT, Allocator, Policy>; is commited now

ktprime avatar Aug 16 '25 13:08 ktprime

Oh,got it. If I have free time I will try to implement. 🫡

xmmmmmovo avatar Aug 16 '25 13:08 xmmmmmovo