2

I'm trying to serialize and deserialize a sparsepp spp::sparse_hash_map of type spp::sparse_hash_map<const std::vector<uint32_t>, uint32_t> using cereal.

See related question on why I use a const key here.

However, compilation of the following code fails:

spp::sparse_hash_map<const std::vector<uint32_t>, uint32_t> my_map;

std::ifstream file(path, std::ios::binary); 
cereal::BinaryInputArchive input(file); 
input(my_map);

When I remove const from the key type, it compiles:

spp::sparse_hash_map<std::vector<uint32_t>, uint32_t> my_map;

std::ifstream file(path, std::ios::binary); 
cereal::BinaryInputArchive input(file); 
input(my_map);

The output of the compiler:

error: no matching function for call to ‘cereal::BinaryInputArchive::processImpl(const std::vector<unsigned int>&)’

Compiler: clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)

Is there a way to deserealize this type using cereal? What am I missing?

Dänu
  • 5,791
  • 9
  • 43
  • 56
  • You don't need to explicitly make the keys `const`, as they will be that anyway. See e.g. [this `std::map` reference](https://en.cppreference.com/w/cpp/container/map) where you will see that the `value_type` is `std::pair`. – Some programmer dude Feb 28 '19 at 08:29
  • @Someprogrammerdude I do, see [here](https://stackoverflow.com/questions/54890095/pointers-to-keys-of-sparsepp-does-the-location-of-the-keys-change). For simplicities sake, I asked about `std::map` here, as it produces the same error as `spp::sparse_hash_map` – Dänu Feb 28 '19 at 08:44
  • But the ordered `std::map` is ***not*** the same as some unordered hash map (not even `std::unordered_map`). `std::map` is ordered by the key, and the ordering is done on insertion only. Attempting to modify they key of a `std::map` is not allowed. If you have a problem with `spp::sparse_hash_map` then please ask about it specifically. One problem with `const any_key_type` for `std::map` is that then you have effectively `const const any_key_type`, and that makes no sense. – Some programmer dude Feb 28 '19 at 08:45
  • 1
    Also, in this question you should probably have a link to your previous question to help explain why you need the key to be `const`. – Some programmer dude Feb 28 '19 at 08:48
  • I will update the question accordingly. However, as I stated, the error is the *same* whether I use `spp::sparse_hash_map` `std::map`, which makes it easier to reproduce for anyone reading. – Dänu Feb 28 '19 at 08:49
  • I suspect the way Cereal extracts the key type from the map is broken, which then makes it create acidentally `const` variables for its own processing. – Quentin Feb 28 '19 at 09:23
  • @Quentin I now suspect there is a general problem with the serialization of `const` types, as I just tried it using [libnop](https://github.com/google/libnop), which fails to compile as well. – Dänu Feb 28 '19 at 10:31

0 Answers0