martinus / unordered_dense

A fast & densely stored hashmap and hashset based on robin-hood backward shift deletion
MIT License
898 stars 72 forks source link

[BUG] overload operator== do not work for unordered_dense::detail::table #75

Closed HailongYang96 closed 1 year ago

HailongYang96 commented 1 year ago

Describe the bug (I am a new fish in C++. If there are some errors, please forgive me :) When I am trying to overload the operator==() for detail::table like below, I found it doesn't work for me. The custom function is not invoked.

template<typename _K, typename _V, typename _Hash, typename _Pred, typename _Allocator, typename _Bucket>auto operator==(ankerl::unordered_dense::detail::table<_K, _V, _Hash, _Pred, _Allocator, _Bucket> const& a, ankerl::unordered_dense::detail::table<_K, _V, _Hash, _Pred, _Allocator, _Bucket> const& b) -> bool {
    cout << "test" << endl;
    return true;
}

I trying to understand why, so I referred to std::map and std::unordered_map. I found the operator== for them is defined as the template in its class. Compared with unordered_dense::detail::table it is not a template function.

// unordered_mapp
template<typename _Key1, typename _Tp1, typename _Hash1, typename _Pred1, typename _Alloc1>
friend bool
operator==(const unordered_map<_Key1, _Tp1, _Hash1, _Pred1, _Alloc1>&,
           const unordered_map<_Key1, _Tp1, _Hash1, _Pred1, _Alloc1>&);

// unordered_dense::detail::table
friend auto operator==(table const& a, table const& b) -> bool;

My questions are:

  1. I wonder if it is not possible for me to overload the operator== for ankerl::unordered_dense::detail::table?
  2. If Q1 is yes, will it be updated to the template like std in the future?

To Reproduce

#include "ankerl/unordered_dense.h"
#include <iostream>
using namespace std;

template<typename _K, typename _V, typename _Hash, typename _Pred, typename _Allocator, typename _Bucket>auto operator==(ankerl::unordered_dense::detail::table<_K, _V, _Hash, _Pred, _Allocator, _Bucket> const& a, ankerl::unordered_dense::detail::table<_K, _V, _Hash, _Pred, _Allocator, _Bucket> const& b) -> bool {
    cout << "test" << endl;
    return true;
}

int main(){

    ankerl::unordered_dense::map<int, int*> a;
    ankerl::unordered_dense::map<int, int*> b;

    if (a == b){ cout << "true" << endl; } // in stdout, the "test" will not be output, so the default operator== was invoked, not the custom one.
    return 0;
}

Expected behavior

System:

martinus commented 1 year ago

operator== is implemented here: https://github.com/martinus/unordered_dense/blob/main/include/ankerl/unordered_dense.h#L1782

HailongYang96 commented 1 year ago

Wow, thanks for your clarification! I am trying to overload the operator== with the template function, but it does not work for me🥹 Could you help with that... I really appreciate that🙏!!

martinus commented 1 year ago

That's a question for https://www.reddit.com/r/cpp_questions/, this is not a bug in the map