Lambda function argument + deduction improvement

Viewed 94

I constructed this template function for a double for loop that has to be executed a lot and used to be done with a macro:

template <typename TYPE_OF_ENTITY, typename LAMBDA_FUNC>
void foreach_t(list faces, LAMBDA_FUNC func)
{
    for(...)
        for(...)
            func(pEnt)
}

This worked really well and could be called:

foreach_t<FACE>(faces,[&](FACE* pFace){...});

Then I wanted to enfore that your lambda has to have TYPE_OF_ENTITY* as an argument. For this I needed a statefull and stateless template function.

template <typename TYPE_OF_ENTITY, typename LAMBDA_RETURN_TYPE>
using functionPtr = LAMBDA_RETURN_TYPE(*)(TYPE_OF_ENTITY*);

template <typename TYPE_OF_ENTITY, typename LAMBDA_RETURN_TYPE>
void foreach_t(list faces, functionPtr<TYPE_OF_ENTITY, LAMBDA_RETURN_TYPE> func)
{
    for(...)
        for(...)
            func(pEnt)
}

template <typename TYPE_OF_ENTITY, typename LAMBDA_RETURN_TYPE>
void foreach_t(list faces, std::function<LAMBDA_RETURN_TYPE(TYPE_OF_ENTITY*)> func)
{
    for(...)
        for(...)
            func(pEnt)
}

They work as I want, only now I have to specify the 2 template arguments since they can't be deduced anymore:

foreach_t<FACE, void>(faces,[&](FACE* pFace){...});

Is there a way to improve this deduction so I only need to pass the function arguments?

If possible it needs to be supported by the visual studio 2013 compatible v2.1 compiler

2 Answers
Related