I have the following template function:
template<typename K, typename V>
bool hasKey( const std::map<K, V>& m, K& k ) {
return m.find(k) != m.end();
}
The keys in the map are not const
.
Now, I may have a const K
. How can I write a template that would allow me to pass in both K and
const K` to the function?
Is the solution to use a const_cast
every time I call the function?
You can achieve what you want with the following
template <typename Key, typename Value, typename K>
bool hasKey(const std::map<Key, Value>& mp, const K& k) {
return mp.find(k) != mp.end();
}
This way you are sure when looking at the declaration of the function that neither operand is going to be modified since they are both references to const
.
Both non-const and const references (and even rvalues) to key types can be passed to the .find()
methods in std::map
. This works because the .find()
method accepts a key type by const reference, so if you pass a non-const reference it gets bound by a const reference anyway, so it doesn't make a difference.
Another thing that you get from templating the key types separately is compatibility with transparent comparators (since C++14, see http://en.cppreference.com/w/cpp/container/map/find). For more on what transparent comparators are see What are transparent comparators?.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With