Use of variadic templates of lambdas ?

Hi my friends.
At first i specify that i use C++11.
Often i need to make some operations before and after a call to a function. This function not have each time a same number of parameters, but one of the parameters i will change in a previous operation. It is possible to pass this central function to another function which will do automatically a previous operations, then call this passed function with parameters, and then after do some other operations ?
I read some useful information in page :
http://stackoverflow.com/questions/3377828/variadic-templates-for-lambda-expressions
but i am still not very clear what i need to make in my case.
Now, i will show how exactly happen in my code :
In one place i have :
1
2
3
4
        auto biggerI = upper_bound(comb.begin(),comb.end(),pi);
        auto piI = comb.insert(biggerI,pi);
        bool testLess = test(possLess,comb,compsNr+1,compsMaxLess);
        comb.erase(piI);

In another place i have :
1
2
3
4
           auto biggerI = upper_bound(comb.begin(),comb.end(),pi);
            auto piI = comb.insert(biggerI,pi);
            writePis();
            comb.erase(piI);

In another place i have :
1
2
3
4
            auto biggerI = upper_bound(comb.begin(),comb.end(),pi);
            auto piI = comb.insert(biggerI,pi);
            getMaxim(length+1);
            comb.erase(piI);


Our understand what i mean ?
I want to make a function that accept a function with differents parameters and do some operations, and call it with :
 
doSomeOperationsAround(function_with_parameters);


thanks for any help, Daniel
Hi my friends.
I not receive any answer, but searching and searching, trying and trying, thinking and thinking, i found a way to do it using variadic templates and lambda functions :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<typename R, typename... A, typename... PA>
R EqSolution3::combCall(int pih, function<R(A...)> f, PA ...args)
{
    auto biggerI = upper_bound(m_comb.begin(),m_comb.end(),pih);
    auto piI = m_comb.insert(biggerI,pih);
    R ret = f(args...);
    m_comb.erase(piI);
    return ret;
}

template<typename O,typename R, typename ...A, typename... PA>
R EqSolution3::combCall(int pih, R (O::*f)(A...),PA ...args)
{
    function<R(A...)> func = [&] (PA ...args) { return (this->*f)(args...);};
    return combCall(pih,func,args...);
}

In order to use it, and call we can write :
 
bool testLess = combCall(pi,&EqSolution3::testR,possLess,compsNr+1,compsMaxLess);

this is very much better as write always something like :
1
2
3
4
        auto biggerI = upper_bound(m_comb.begin(),m_comb.end(),pi);
        auto piI = m_comb.insert(biggerI,pi);
        bool testLess = testR(possLess,compsNr+1,compsMaxLess);
        m_comb.erase(piI);


Thanks for try it. Daniel
Last code have some problem, it pass always the values by copy.
Perfeccioned this look like :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<typename R, typename... A, typename... PA>
R EqSolution3::combCall(list<int>& comb, int pih, function<R(A...)> f, PA&&... args)
{
    auto biggerI = upper_bound(comb.begin(),comb.end(),pih);
    auto piI = comb.insert(biggerI,pih);
    R ret = f(std::forward<PA>(args)...);
    comb.erase(piI);
    return ret;
}

template<typename O,typename R, typename ...A, typename... PA>
R EqSolution3::combCall(list<int>& comb, int pih, R (O::*f)(A...), PA&&... args)
{
    function<R(PA...)> func = [&] (PA ...args) { return (this->*f)(args...);};
    return combCall(comb,pih,func,std::forward<PA>(args)...);
}


Daniel
Topic archived. No new replies allowed.