mercredi 6 décembre 2017

map with fixed (const) keys and changeable data? [duplicate]

This question already has an answer here:

I have map like this:

struct cmp_str
{
   bool operator()(char const *a, char const *b)
   {
      return std::strcmp(a, b) < 0;
   }
};
std::map<char *, std::atomic<bool>, cmp_str> my_map;

I add keys to my_map only during creation, this is important because of I use my_map from multiply threads without locking.

I want compiler to check that nobody add keys to my_map after creation.

But I can not define my_map as const, because of then I can not change data. Is it possible somehow to make half const map?

I mean it have fixed number of keys and all them defined during creation, but it is possible to change data (not keys) in map?

I thought about holding std::atomic<bool> *const in const std::map, but I have to manually free memory after this. Smart pointers like std::unique_ptr not helps here.

Any ideas how to make half const map with help of std:: from c++11?

Aucun commentaire:

Enregistrer un commentaire