I have the following setup:
typedef std::function<void()> reaction;
class Node
{
public:
...
private:
void connect();
void receive();
private:
const std::map<std::pair<Status, Event>, reaction> TransTable = {
{{DISCONNECTED, CONNECT}, &Node::connect},
{{CONNECTING, RECEIVE}, &Node::receive}
};
}
But I always get the error:
error: could not convert from <brace-enclosed initializer list> to const std::map<std::pair<Status, Event>, std::function<void()> >
What is wrong with my initalizer list?
Your question is missing a MCVE, however the error message is clear enough:
reaction
seems to be a typedef forstd::function<void()>
. A member function pointer such as&Node::connect
cannot be converted to astd::function<void()>
, because the latter lacks any kind ofthis
parameter upon which to actually call the function.You can, however, use lambdas to capture and store the instance that is currently initializing this
TransTable
member: