mardi 1 mars 2016

Using std::bind in a switch on type situation

I have a situation where I need to do some switching on some input flag and I want to either do a binary comparison or a unary operation on some input function object. So for example

template <typename Func>
void do_something(Object one, Object possibly_not_here, Func function) {
    switch(flag) {
        case 1:
            call_function_with_one_argument(one.arg);
        case 2:
            call_function_with_two_arguments(one.some_other_member, possibly_not_here.some_other_member);
    }
}

I am planning on passing a lambda to this function which will either accept two arguments or just one argument. The possibly_not_here parameter may not be passed to this function. This will work because the function object that I pass in will be templated so it will be able to handle different types.

Is there a way I can achieve this efficiently with std::bind() or variadic templates? I cannot think of a way to do this that would not duplicate code...

Aucun commentaire:

Enregistrer un commentaire