I have encountered some strange behavior.
This code gives me errors:
struct Game {
void stop() {std::cout << "success\n";}
};
template<class ...Args>
struct holder {
std::map<std::string, std::function<void(Args...)>> funcMap;
template <typename T, typename U>
void connect(T* targObj, const std::string& funcName) {
std::function<void(Args...)> newFunc = std::bind(U, targObj);
//expected primary expression before ',' token on line above
funcMap[funcName] = newFunc;
}
void invoke(const std::string& funcName, class Args...Vals)
{funcMap[funcName](Vals...);}
};
int main() {
holder<> h;
Game g;
h.connect<Game, &Game::stop>(g, "close");
h.invoke();
}
It seems that std::bind
does not like typenames as inputs. Is there a workaround for this? It works fine to manually use std::bind
with the same parameters, but it is more boilerplate code:
std::function<void(Args...)> newFunc = std::bind(&ae::Game::stop, targObj);
Even without any input, it still errors on compile. Why doesn't my function work?
EDIT: Thanks for the help. As it turns out, bind does not accept a typename, it accepts an address.
I guess the function is static ? If not you have the solution, it needs an object to be called.
But bind is trying to call
g
with argument"close"
on no object, can t work if not static