Is this an appropriate use of std::function?

Hi

I have a function F1 which gets certain data and performs operations on this data. The function gets data using one of two different functions (F2 or F3) but performs exactly the same operations on that data.

Currently, F2 and F3 are supplied as parameters to F1 (as an std::function). Is this an appropriate use of std::function or is it overkill here given that only F2 and F3 can be supplied? Would it be better, for example, to pass an enum and then just use an if statement to call either F2 or F3?

Thanks
It depends on whether F1() should know about F2() and F3().
That aside, are you just passing normal functions to the std::function constructor? std::function is a really powerful class, and as such constructing and calling it has a non-negligible cost. Consider passing a function pointer instead, or rewriting the F1() as a template:
1
2
3
4
5
6
template <typename T>
whatever_t F1(const T &f){
    //...
    f(foo);
    //...
}
This F1() accepts normal functions as well as lambdas, without the cost of std::function.
Topic archived. No new replies allowed.