I am writing a program in C++ that I should embed different functions into a same for loop.
Examples:
for(i = 0; i < N_ITER; i++) {
    /* ... */
    function_A(); 
    /* ... */
}
for(i = 0; i < N_ITER; i++) {
    /* ... */
    function_B(); 
    /* ... */
}
For performance consideration I must inline function_A and function_B into this function. My first solution is using Functors and function templates as follows:
class function_A {
public:
    void operator()() {
        /* ... */   
    }
};
class function_B {
public:
    void operator()() {
        /* ... */
    }
};
template <class T>
class run {
public:
    void operator()() {
        /* ... */
        T func;
        for (i = 0; i < N_ITER; i++) {
            /* ... */
            func();
            /* ... */
        }
        /* ... */
    }
};
And I can call the functions like:
run<function_A>()();
run<function_B>()();
But soon I found that there's too much duplicated functor definations class xxx { public: void operator()() {...} }; in code and it looks awkward.
So I turned into a solution using lambda:
auto function_A = []()
{
    /* ... */
};
auto function_B = []()
{
    /* ... */
};
template <class T>
class run {
public:
    T func;
    run(T func) : func(func) {}
    void operator()() {
        /* ... */
        for (i = 0; i < N_ITER; i++) {
            /* ... */
            func();
            /* ... */
        }
        /* ... */
    }
};
But this time its harder to call these functions:
run<decltype(function_A)> funcA(function_A); funcA();
run<decltype(function_A)> funcB(function_A); funcB();
And it is not as clear as the previous solution.
It there a more elegant way to implement it in C++? Am I missing something? Any suggestions would be appreciated !!!
 
     
     
    