Help compacting Functions

Pages: 12
It's still fairly verbose:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
typedef std::string (*function_pointer0)();
typedef std::string (*function_pointer1)(const std::string &);
typedef std::string (*function_pointer2)(const std::string &, const std::string &);
//etc.

std::map<std::string, function_pointer0> function_map0;
std::map<std::string, function_pointer1> function_map1;
std::map<std::string, function_pointer2> function_map2;
//etc.

//...

//Note: you can't put these blocks in a loop because the types are heterogeneous.
if (args.size() == 0){
    auto it = function_map0.find(command);
    if (it == function_map0.end())
        //not found
    return (*it)();
}
if (args.size() == 1){
    auto it = function_map1.find(command);
    if (it == function_map1.end())
        //not found
    return (*it)(args[0]);
}
if (args.size() == 2){
    auto it = function_map2.find(command);
    if (it == function_map2.end())
        //not found
    return (*it)(args[0], args[1]);
}
//etc. 
Last edited on
You can pack your n < max_args arguments into a tuple of max_args elements. You could use a "null" type to fill the rest of the elements of the tuple. You should be able to call a function with only the first n elements of that tuple.

I'm away from my compiler right now, but I'll see if I can hack some code together when I get back home.
Last edited on
Topic archived. No new replies allowed.
Pages: 12