I am trying to write a function, which will give me a default value when the key is not inside a std::map. In all cases will my default value be numerical_limit::infinity(). Hovewer this simple example is not working.
#include <iostream>
#include <map>
#include <limits>
template<typename KeyType, typename ValueType>
ValueType mapDefaultInf(const std::map<KeyType, ValueType> & map, const KeyType & key)
{
if(!map.contains(key))
{
return std::numeric_limits<ValueType>::infinity();
}
else
{
return map[key];
}
}
int main()
{
std::map<std::string, int> map;
auto el = mapDefaultInf(map, "alexey");
std::cout << el << std::endl;
return 0;
}
Error is:
main.cpp:29:42: error: no matching function for call to ‘mapDefaultInf(std::map, int>&, const char [7])’
Can someone help me understand the error.
Thanks in advance.