mardi 10 août 2021

How to force the const find to be called

I'm coding with std::map in C++11.

I've read this link, which told me that the C++11 standard guarantees that const method access to containers is safe from different threads.

As my understanding, it means that std::map::size() is thread-safe, because this function is declared as size_type size() const noexcept;.

Now I want to call the function std::map::find thread-safely. For example, if (mp.find(xxx) != mp.end()) {}. However, there are two versions of find, one is const and the other is non-const:https://en.cppreference.com/w/cpp/container/map/find.

So how do I know which version of find is calling? How can I force the const-version find to be called in order to get a thread-safe code?

I knew there was a const version of std::map::cend(), will if (mp.find(xxx) != mp.cend()) {} work as expected?

Aucun commentaire:

Enregistrer un commentaire