Three things are required for something like this to happen:
The map's comparator must be a transparent comparator (requires C++14, but you're already using string_view
which is C++17, so this is a moot point).
The at()
method must have an overload that participates in overload resolution when the container has a transparent comparator.
the parameter must be convertible to the map's key_type
.
Neither of these are true in your example. The default std::less
comparator is not a transparent comparator, there is no such overload for at()
, and std::string
does not have an implicit conversion from std::string_view
.
There's nothing you can do about at()
, however you can do something about the comparator namely using the (transparent std::void
comparator), and then use find()
instead of at()
, which does have a suitable overload:
#include <map>
#include <string>
#include <string_view>
int main()
{
std::string_view key{ "a" };
std::map<std::string, int, std::less<void>> tmp;
tmp["a"] = 0;
auto iter=tmp.find(key);
}
More complete demo