I have the following functor which wraps another functor or lambda function and automatically sets an index parameter. An example will explain best. I can do the following:
auto f = stx::with_index([](int a, int index){ std::cout << a << " " << index << std::endl; });
f(5);
f(3);
f(9);
Output:
5 0
3 1
9 2
Here is the functor code:
template<class FUNC>
class IndexFunctor
{
public:
typedef FUNC FUNC_T;
explicit IndexFunctor(const FUNC_T& func) : func(func), index(0) {}
template<class... ARGS>
void operator ()(ARGS&&... args)
{
func(args..., index++);
}
const FUNC_T& GetFunctor() const
{
return func;
}
int GetIndex() const
{
return index;
}
void SetIndex(int index)
{
this->index = index;
}
private:
FUNC_T func;
int index;
};
template<class FUNC>
IndexFunctor<FUNC> with_index(const FUNC& func)
{
return IndexFunctor<FUNC>(func);
}
Now the problem is I want to use it with functions that may return a value. For example
auto f = stx::with_index([](int a, int index){ return a * index; });
int a = f(5);
But I cannot figure out how to modify my functor to get this to work. I would like the functor to be compatible with both functions that return a value and those that don't automatically.
Can anyone offer some suggestions? Thanks!
I am using VS2012 Microsoft Visual C++ Compiler Nov 2012 CTP