Home > Net >  How to make a function dispatch table with a two dimensional map
How to make a function dispatch table with a two dimensional map

Time:04-06

I want to make a function dispatch table with a 2 dimensional map but i can't figure out how to make a brace enclosed list.

enum state_t{
OPENED,
CLOSED,
OPENING,
CLOSING,
}

state_t do_something(instance_data *data);
state_t do_bar(instance_data *data);
std::map<const state_t, std::map<const state_t, std::function<state_t(instance_data *data)>>> state_table
{
 {{OPENED, OPENED}, do_something},
 {{OPENED, CLOSING}, do_bar}
};

CodePudding user response:

You seem misunderstood smth. The proper initializer list for map in map should look like bellow. const for the key type argument is odd, the key type is const inside the map.

std::map<state_t, std::map<state_t, std::function<state_t(instance_data *data)>>> state_table
{{ OPENED, {
    {OPENED, do_something},
    {CLOSING}, do_bar}
 }}
};

However you might want to use std::pair or std::tuple

std::map<std::pair<state_t, state_t>, std::function<state_t(instance_data *data)>> state_table
{
  {{ OPENED, OPENED }, do_something},
  {{ OPENED, CLOSING}, do_bar}
};
  •  Tags:  
  • c
  • Related