I'm building an application where the interface is like a shell. The idea is to write the function name and the function is called. I saw a post in stackoverflow (see below) that explains a way to do this storing the function pointer.
I'm storing my functions in a map of type std::map<std::string, std::function<void(const std::vector<std:.string>, const std::map<std:.string, std::string>)> > but when I compile it gives the follwing error
error: could not convert ‘{{"exit", ((GitDeploy::Shell*)this)->GitDeploy::Shell::exit}, {"help", ((GitDeploy::Shell*)this)->GitDeploy::Shell::help}, {"dbinfo", ((GitDeploy::Shell*)this)->GitDeploy::Shell::dbinfo}, {"list", ((GitDeploy::Shell*)this)->GitDeploy::Shell::list}, {"create", ((GitDeploy::Shell*)this)->GitDeploy::Shell::create}, {"update", ((GitDeploy::Shell*)this)->GitDeploy::Shell::update}, {"remove", ((GitDeploy::Shell*)this)->GitDeploy::Shell::remove}}’ from ‘<brace-enclosed initializer list>’ to ‘const std::map<std::basic_string<char>, std::function<void(std::vector<std::basic_string<char> >, std::map<std::basic_string<char>, std::basic_string<char> >)> >’
         };
shell.h
#ifndef SHELL_CLASS
#define SHELL_CLASS
#include <string>
#include <map>
#include <functional>
#include <vector>
namespace GitDeploy {
    class Shell {
        private:
            std::map<std::string, std::function<void(const std::vector<std::string>, const std::map<std::string, std::string>)> > const methods {
                {"exit", exit},
                {"help", help},
                {"dbinfo", dbinfo},
                {"list", list},
                {"create", create},
                {"update", update},
                {"remove", remove}
            };
            std::map<std::string, std::string> const method_help {
                {"exit", "Exists GitDeploy"},
                {"help", "Prints available commands"},
                {"dbinfo", "Prints database information"},
                {"list", "Lists repositories."},
                {"create", "Adds a new repository"},
                {"update", "Updates a existing repositor\nyou must pass is uuid or partial uuid and the values to update\nupdate <uuid> path=<path> reset=<reset>"},
                {"remove", "Deletes a repository\nTo delete a repository, pass the uuids or partial uuids to delete\ndelete <uuid> <uuid> [...]"}
            };
            bool exit_shell;
        public:
            void serve();
            void exit(const std::vector<std::string>, const std::map<std::string, std::string>);
            void help(const std::vector<std::string>, const std::map<std::string, std::string>);
            void dbinfo(const std::vector<std::string>, const std::map<std::string, std::string>);
            void list(const std::vector<std::string>, const std::map<std::string, std::string>);
            void create(const std::vector<std::string>, const std::map<std::string, std::string>);
            void update(const std::vector<std::string>, const std::map<std::string, std::string>);
            void remove(const std::vector<std::string>, const std::map<std::string, std::string>);
    };
}
#endif
Answer to StackOverflow post
#include <iostream>
#include <map>
int add(int i, int j) { return i+j; }
int sub(int i, int j) { return i-j; }
typedef int (*FnPtr)(int, int);
int main() {
    // initialization:
    std::map<std::string, FnPtr> myMap;
    myMap["add"] = add;
    myMap["sub"] = sub;
    // usage:
    std::string s("add");
    int res = myMap[s](2,3);
    std::cout << res;
}
Thanks,
Hugo Rodrigues
