0

I have a function, let's say

void processSomething(Arg1 arg1, Function t){
    ...
    t(someVariable);
}

I want both of the following usages to work:

processSomething(myArg1, [&](SomeVariable someVar){...});
void(*myFunc)(void) = &someFunc;
processSomething(myArg1, myFunc);

However, I found that I can't use the lambda-way when using void(*myFunc)(void) as parameter declaration. Any way to have both usages work without two separate functions or an overly complicated use of wrappers?

1

1 Answer 1

6

Well, you have two choices:

  1. Templates:

    template<class F>
    void processSomething(Arg1 arg1, F t){
    

    This is the preferred way as it creates more efficient code, but at the cost of possible code-duplication.

  2. Using a std::function or such:

    void processSomething(Arg1 arg1, std::function<void(SomeVariable)> t){
    

    There's a runtime-cost to the indirections involved, but it will use the same code in each case.

Sign up to request clarification or add additional context in comments.

Comments

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.