c++ - Using std::function in member initialization list -
i have typedef:
typedef s32(imydataclass1::*getdatafunction_t)(void); and type:
struct functionmap_t { std::vector<getdatafunction_t> pdatafunctiontable; struct datarequestor_t datarequestor; }; i have member variable:
functionmap_t fnmap1; which set in member initializer list:
myclass::myclass() : fnmap1({ { &imydataclass1::getdata1, &imydataclass1::getdata2, &imydataclass1::getdata3 }, datarequestor1 }) which use in constructor:
addnewfnmap(fnmap1); this works fine. unfortunately, it's not extendable function pointers different classes, getdatafunction_t bound pointers in imydataclass1
i tried using templates, ran issues not around. i'm trying use std::function, means change original typedef (i think):
typedef std::function<s32(void)> getdatafunction_t; what correct way set initializer list in situation? use std::bind here?
update: here attempt @ new initializer list. have no idea if correct, had right before posting question:
{ { (std::bind(&imydataclass1::getdata1, functiontofetchcorrectobject())), (std::bind(&imydataclass1::getdata2, functiontofetchcorrectobject())), (std::bind(&imydataclass1::getdata3, functiontofetchcorrectobject())) }, pgn65370 }
you use lambda expressions. however, if want keep signature std::function<s32(void)>, you'll need capture object.
myclass::myclass() : fnmap1({ { [this](){ return getdata1(); }, [this](){ return getdata2(); }, [this](){ return getdata3(); }, }, datarequestor1 }) if don't want this, change signature pass this pointer parameter callbacks. barry points out in comments, not allow put callbacks different classes same vector. reinterpret_cast pointer inside lambda besides being ugly , dangerous, how caller know pointer pass in?
Comments
Post a Comment