Home > Software engineering >  Find std::function by string key in unordered_map
Find std::function by string key in unordered_map

Time:02-11

This works:

std::unordered_map<std::string, int> m = {};
auto c = m.find(typeName);
if (c == m.end())
{

}

This works:

std::unordered_map<std::string, std::string> m = {};
auto c = m.find(typeName);
if (c == m.end())
{

}

This doesn't work:

std::unordered_map<std::string, std::function<void>> m = {};
auto c = m.find(typeName);
if (c == m.end())
{

}

== shown as error:

In template: implicit instantiation of undefined template 'std::function' error occurred here (declaration of _T2 second inside inside pair)

in instantiation of template class 'std::pair<const std::string, std::function>' requested here

in instantiation of template class 'std::__hash_value_type<std::string, std::function>' requested here template is declared here

P.S. is there any other way storing std::function by string key if there is no way making it work?

CodePudding user response:

std::function expects a function type as template argument, while void is not. For example if the function takes nothing and returns void then it should be

std::unordered_map<std::string, std::function<void()>> m = {};
  • Related