Is there a way to easily convert a unordered_map<K, shared_ptr<T>> to unordered_map<K, shared_ptr<const T>>?

Viewed 39

As the title suggests, I would like to know if there is a way to convert a std::unordered_map<K, std::shared_ptr<T>> to a std::unordered_map<K, std::shared_ptr<const T>> that would allow me to do something like this:

class KeyClass;
class ValueClass;

void f(const std::unordered_map<KeyClass, std::shared_ptr<const ValueClass>>& map)
{
    // do whatever but make sure that absolutely nothing changes
}

int main()
{
    std::unordered_map<KeyClass, std::shared_ptr<ValueClass>> map;
    // init the map
    f(map);
}

The reason I am asking is that I made sure that at least the conversion from std::shared_ptr<T> can be implicitly converted to std::shared_ptr<const T>.

What I am conceptually aiming at is to make sure that nothing in the map argument and itself will be changed. Is this overkill in any way ?

0 Answers
Related