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
If all you need to do is to check if lambda takes value of type TYPE_OF_ENTITY*
as parameter you don't really need to split function into two separate overloads (for statefull and stateless lambda). You could simply check if it can be called with this kind of parameter e.g.:
#include <type_traits> // for std::declval
template <typename TYPE_OF_ENTITY, typename LAMBDA_FUNC>
auto foreach_t(list faces, LAMBDA_FUNC func) -> decltype(func(std::declval<TYPE_OF_ENTITY*>()), void())
{
for(...)
for(...)
func(pEnt)
}