Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a function dispatch table with a two dimensional map

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}
};

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading