0

Lets say I have 10 templated functions, for example:

command1<T>(const std::string&) 
command10<T>(const std::string&, int timeInSeconds)

At a point in time in my code I will establish that I wish to execute a particular command. I will then request information regarding the type associated with this command (at runtime), which is returned to me via an enum. So I establish that I wish to execute command2 and the enum contains STRING. I therefore wish to call:

command2<std::string>(id, param1, param2); 

What would you recommend as a good approach for doing this mapping?

The enum can contain INT, BOOL; DOUBLE or STRING. The arguments passed to a particular command are not dependant on the enum's value.

Example:

Here's an example to explain a little better:

Lets say my program receives "command4 a" from the command line. I parse this input and establish that I need to call command4. I then lookup the type associated with "a" and in this case get INT. I now need to call command4<int>("a");

3
  • What do you mean by "request information regarding the associated type"? Does this involve lookup at runtime? Commented Sep 18, 2012 at 11:29
  • @honk Yes, at runtime I establish the type via an enum. Commented Sep 18, 2012 at 11:32
  • 1
    If you only have that information available at runtime, you can only resolve this at runtime. Have you tried a lookup table containing all possible mappings? Commented Sep 18, 2012 at 11:41

1 Answer 1

2

A switch statement will work here:

switch (type) {
    case INT: commmand4<int>(id); break;
    ...
}

Depending on how you're actually calling the methods, templating on the arguments may be a good idea:

template<typename F, typename... Args> void call_function(Type type, Args... &&args) {
    switch (type) {
        case INT: return F::command<int>(std::forward(args)...);
        ...
    }
}

Note that because you can't pass a function template to a function, you'll have to wrap up the function templates inside a class:

struct Command1 {
    template<typename T> static void command(const std::string&);
};
...

call_function<Command1>(INT, "a");
Sign up to request clarification or add additional context in comments.

4 Comments

I have 10+ types and 10+ commands. I will have a lot of duplication since the arguments to the functions are not dependant on the type. But maybe this is the only way to do this...
@Baz you didn't mention that requirement. You can deal with that by templating on the argument types.
I don't understand. In my example, command4 takes a string argument but is called via command4<int>("a") since the enum contains an INT value. If the enum instead contained a BOOL, I would need to call command4<bool>("a") instead. The arguments are not effected.
I think ecatmur is right, but it is probably a big conceptual leap for the OP. Ideally I think starting with a fully expanded example and then showing how to refactor with templates would be clearest, but it would be a long post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.