-1

Apparently my previous question was an XY question. So I'm re-framing it here.

I have a class which will need to call a different function to handle an object argument depending on a variable set in it's constructor.

void functionCandidate1(const Type1 & arg1, Type2 & arg2) {// do something}
void functionCandidate2(const Type1 & arg1, Type2 & arg2) {// do something different}

class MyClass{
    bool use_function1_;
    MyClass(const bool & use_function1): use_function1_(use_function1){
    }

    void run(const Type1 & arg1){
        Type2 obj;
        if (use_function1_){
            functionCandidate1(arg1, obj);
        }else{
            functionCandidate2(arg1, obj);
        }
    }
};

The arguments to each of the functions will be the same but the behavior will be different internally. If possible I would like to avoid calling an if statement on every execution of MyClass::run

Morten Nissov
  • 392
  • 3
  • 13
  • Any reason not to use a function pointer? Seems straightforward. – john Apr 27 '23 at 16:33
  • This question is totally different from the original. It should work as is. Now your two functions have same signature and you can simply call either of them depending on the value of the `bool` – 463035818_is_not_an_ai Apr 27 '23 at 16:35
  • and because now the two functions have same signature you could also use a function pointer. Or now you could also use polymorphism. – 463035818_is_not_an_ai Apr 27 '23 at 16:36
  • Can you please show some real, actual code instead of made-up code? No self-respecting C++ compiler will compile that constructor, exactly as shown. – Sam Varshavchik Apr 27 '23 at 16:38
  • Same comment as on your previous question. Use an appropriate `std::function` member, and initialize it at the constructor with a lambda expression calling the candidates conditionally. – πάντα ῥεῖ Apr 27 '23 at 16:39
  • The point is I want to avoid the if statement. Also `arg2` being the same type is a simplification, it doesn't necessarily need to be this way. I should figured I would present a slightly simpler condition. – Morten Nissov Apr 27 '23 at 16:39
  • why do you want to avoid the `if` ? Its not a big difference whether you branch on `if` or on a function pointer – 463035818_is_not_an_ai Apr 27 '23 at 16:40
  • @SamVarshavchik made a typo, fixing the initializer list. Real code is too long with too many non-standard libraries, would really just detract from the problem. – Morten Nissov Apr 27 '23 at 16:41
  • @463035818_is_not_a_number because it should be unnecessary no? Given that the condition will never change for the life of the class. – Morten Nissov Apr 27 '23 at 16:41
  • Well, looks like someone else told you what the solution is: use a function pointer or `std::function` and initialize it in the constructor. Which part of this solution was unclear? – Sam Varshavchik Apr 27 '23 at 16:42
  • `arg2` being same type is a simplification that makes the answer to the quesiton you asked not an answer to your actual issue. When I suggested the other question would be a XY problem, I tried to push you to telling you us more about the actual problem, the use case you need this for, not less – 463035818_is_not_an_ai Apr 27 '23 at 16:42
  • " Given that the condition will never change for the life of the class." so what? You should read about branch prediction – 463035818_is_not_an_ai Apr 27 '23 at 16:43
  • @463035818_is_not_a_number you're right, this needs more detail. I didn't understand that as being your point regarding the xy problem comment. I'll remedy that – Morten Nissov Apr 27 '23 at 16:56
  • @MortenNissov See [this example](https://godbolt.org/z/c46KGssnz). Is this what you are trying to achieve, where the types are different (maybe even the number of parameters)? – PaulMcKenzie Apr 27 '23 at 16:56

1 Answers1

0

Not sure if this is a solution for your actual problem, because the quesiton you ask here is totally different from your previous one. And the obvious way, using a function pointer, just works.

struct Type1 {};
struct Type2 {};

void functionCandidate1(const Type1 & arg1, Type2 & arg2) {}
void functionCandidate2(const Type1 & arg1, Type2 & arg2) {}

class MyClass{
    using func_t = void(*)(const Type1&,Type2&);
    
    bool use_function1_;
    func_t function;
    MyClass(const bool & use_function1) : use_function1_(use_function1), function(use_function1? functionCandidate1 : functionCandidate2)
    {}

    void run(const Type1 & arg1){
        Type2 obj;
        function(arg1,obj);
    }
};

You should consider polymorphism however, with a virtual function, because this looks just like that.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185