I would like to understand why this fails:
template <class T, class U> T apply(U stuff, std::function<T (U)> function) { return function(stuff); }
(Of course, this is not real code).
In g ++ - 4.8, I get "template argument 1 is not valid".
Thanks!
Edit: detailed example: Basically, I want to provide a specific prototype of the MapFunction and ReductionFunction types.
I would like to:
- MapFunction: typeof (* InputIterator) -> T
- ReductionFunction: (T, T) โ T
the code:
template <class T, class InputIterator, class ReductionFunction> T mapReduce_n(InputIterator in, unsigned int size, T baseval, std::function<T (decltype(*InputIterator))> map, ReductionFunction reduce) { T val = baseval; #pragma omp parallel { T map_val = baseval; #pragma omp for nowait for (auto i = 0U; i < size; ++i) { map_val = reduce(map_val, map(*(in + i))); } #pragma omp critical val = reduce(val, map_val); } return val; }
Edit 2:
I think the std::function<T (decltype(*InputIterator))> map is wrong, it should be: std::function<T (decltype(*in))> map .
However, this fails:
mismatched types 'std::function<T(decltype (* in))>' and 'double (*)(std::complex<double>)'
I also tried iterator features:
std::function<T (std::iterator_traits<InputIterator>::value_type)> map
But he fails:
type/value mismatch at argument 1 in template parameter list for 'template<class _Signature> class std::function' error: expected a type, got '(T)(std::iterator_traits<_II>::value_type)'
Third edit:
Another test, I think, I'm starting to get closer!
std::function<T (typename std::iterator_traits<InputIterator>::value_type)> map
Failure:
mismatched types 'std::function<T (typename std::iterator_traits<_II>::value_type)>' and 'double (*)(std::complex<double>)'
Here is my call:
MathUtil::mapReduce_n( in, // const std::complex<double> * const conf.spectrumSize(), // unsigned int 0.0, MathUtil::CplxToPower, // double CplxToPower(const std::complex<double> val); std::plus<double>())