c++ - Does std::unordered_map<std::string,std::function<void(std::string&) can hold only static functions? -
this continuation of question c++ function ptr in unorderer_map, compile time error
i trying use std::function
instead function pointer, , can insert function if functions static. otherwise following error
main.cpp:15:11: error: no matching member function call 'insert'
map.insert(std::make_pair("one",&example::processtring));
#include<string> #include <unordered_map> #include<functional> namespace test { namespace test { class example { public: example() { map.insert(std::make_pair("one",&example::processtring)); } static void processtring(std::string & astring) //void processtring(std::string & astring) -> compiler error { } static void processstringtwo(std::string & astring) { } std::unordered_map<std::string,std::function<void(std::string&)>> map; }; } } int main() { return 0; }
in context, std::function
type wrong. member function example::processstring(std::string&)
:
std::function<void(example*, std::string&)>
however, can avoid , "eat up" this
parameter binding:
using std::placeholders; map.insert(std::make_pair("one", std::bind(&example::processstring, this, _1));
now argument left unbound string reference, type can remain:
std::function<void(std::string&)>
Comments
Post a Comment