これが失敗する理由を理解したい:
template <class T, class U>
T apply(U stuff, std::function<T (U)> function) { return function(stuff); }
(もちろん、これは実際のコードではありません)。
g++-4.8 で、「テンプレート引数 1 が無効です」と表示されます。
ありがとう !
編集: 完全な例: 基本的に、私がやりたいことは、MapFunction
andReductionFunction
型に特定のプロトタイプを適用することです。
をお願いします:
- MapFunction : typeof(*InputIterator) -> T
- リダクション関数 : (T, T) -> T
コード:
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;
}
編集2:
std::function<T (decltype(*InputIterator))> map
その部分は、間違っている
と思いますstd::function<T (decltype(*in))> map
。
ただし、これは失敗します:
mismatched types 'std::function<T(decltype (* in))>' and 'double (*)(std::complex<double>)'
イテレータの特性も試しました:
std::function<T (std::iterator_traits<InputIterator>::value_type)> map
しかし、それは失敗します:
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)'
3番目の編集:
別の試練、私は近づき始めていると思います!
std::function<T (typename std::iterator_traits<InputIterator>::value_type)> map
で失敗します:
mismatched types
'std::function<T (typename std::iterator_traits<_II>::value_type)>'
and
'double (*)(std::complex<double>)'
ここに私の電話があります:
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>())