I think you can just create a VoidReducer class, but instead of return reducer.value; you will need return reducer.getvalue(); . Then you just do void VoidReducer::getvalue(){} .
I have not tested this, but the idea should work. You are allowed to return f(); if both f and the current function are of return type void .
EDIT
Now that I have carefully read the question, I see that the problem you are asking is the reducer(functor(elem)); line reducer(functor(elem)); .
To do this, I sent time to compile based on decltype(functor(elem)) .
template <class Functor, class Reducer, class Elem> void Combine(Functor functor, Reducer & reducer, Elem elem, std::true_type) { functor(elem); } template <class Functor, class Reducer, class Elem> void Combine(Functor functor, Reducer & reducer, Elem elem, std::false_type) { reducer(functor(elem)); } template <class Functor, class Reducer, class Elem> void Combine(Functor functor, Reducer & reducer, Elem elem) { Combine(functor, reducer, elem, std::is_same<decltype(functor(elem)), void>()); }
Then calling Combine instead of reducer(functor(elem)) will correctly reduce the return value of functor if and only if it is not empty.
PS: Sprinkle links and std::forward calls to taste.
source share