martinus / robin-hood-hashing

Fast & memory efficient hashtable based on robin hood hashing for C++11/14/17/20
https://gitter.im/martinus/robin-hood-hashing
MIT License
1.52k stars 146 forks source link

unordered_flat_map does not support non-copyable types? #120

Closed luinx closed 3 years ago

luinx commented 3 years ago
#include "robin_hood.h"
#include <string>

class A {
private:
  std::string x;
public:
  A() = delete;
  A& operator=(const A& other) = delete;
  A(A&& other) : x(std::move(other.x)){}
  explicit A(std::string x) : x(std::move(x)) {}
};

int main(int argc, char** argv) {
  robin_hood::unordered_flat_map<std::string, A> m;
  m.emplace("ok", "hi");
  return 0;
}

From the document it seems the choice over flat map and node map is from memory concerns, otherwise, they can be used interchangeably. But the above code just can't compile in flat map mode. only unordered_map works.

martinus commented 3 years ago

Hi, your class needs to be move constructible, and also needs move assignable. Add this method, then it should work:

    A& operator=(A&& other) {
        mVal = std::move(other.mVal);
        return *this;
    }