简体   繁体   中英

C++ custom template with unordered_map

I am trying to create a custom template, and have such code:

template <typename T>
struct Allocator {
    typedef T value_type;
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;

    T *allocate(size_type n, const void *hint=0);

    T *allocate_at_least(size_type n);

    void deallocate(T *p, size_type n);
};
template <class T, class U>
bool operator==(const Allocator<T>&, const Allocator<U>&) {
    return true;
}

int main() {
    using T = long int;

    std::unordered_map<
        T,
        T,
        std::hash<T>,
        std::equal_to<T>,
        Allocator< std::pair<const T, T> >
    > a;
}

It works with vector, but it fails somewhere inside the templates when I use unordered_map. Can you help me to figure out what I am doing wrong? Here is the error:

error: no matching constructor for initialization of 'std::__detail::_Hashtable_alloc<Allocator<std::__detail::_Hash_node<std::pair<const long, long>, false>>>::__buckets_alloc_type' (aka 'Allocator<std::__detail::_Hash_node_base *>')

And link to code: https://godbolt.org/z/zje3EGjb6

PS If I replace Allocator to std::allocator everything works fine.

It needs to have a default constructor and a constructor that is like a copy constructor but parametrized on a non-T type. The following compiles.

template <typename T>
struct Allocator {
    typedef T value_type;
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;

    T* allocate(size_type n, const void* hint = 0) {
        return nullptr;
    }

    T* allocate_at_least(size_type n) {
        return nullptr;
    }

    void deallocate(T* p, size_type n) {
    }

    Allocator() {} // <= this

    template <class U>
    Allocator(const Allocator<U>&) {} // <= and this
};

template <class T, class U>
bool operator==(const Allocator<T>&, const Allocator<U>&) {
    return true;
}

int main() {
    using T = long int;

    std::unordered_map<
        T,
        T,
        std::hash<T>,
        std::equal_to<T>,
        Allocator< std::pair<const T, T> >
    > a;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM