我需要将std::plus<>()
作为谓词传递给一个函数,该函数可以进行类似的积累,但我不太确定如何实现这一点。我想将std::plus
传递给的模板参数是:
Pred && op
当我尝试使用std::plus作为op
时
return detail::reduce(..., std::plus<>()); //.... op = std::plus<>()
但这不起作用,我得到了错误的error C2976: 'std::plus' : too few template arguments
,我应该看到它即将到来,但我不知道如何处理这个问题,并正确传递加法。
我使用op的一个上下文是:
std::accumulate(first, last, std::forward<T>(init),
std::forward<Pred>(op)));
detail::reduce
的过载之一
template <typename ExPolicy, typename InIter, typename T, typename Pred>
typename detail::algorithm_result<ExPolicy, T>::type
reduce_seq(ExPolicy const&, InIter first, InIter last, T && init,
Pred && op)
{
try {
return detail::algorithm_result<ExPolicy, T>::get(
std::accumulate(first, last, std::forward<T>(init),
std::forward<Pred>(op)));
}
catch(std::bad_alloc const& e) {
boost::throw_exception(e);
}
catch (...) {
boost::throw_exception(
hpx::exception_list(boost::current_exception())
);
}
}
发布于 2014-06-06 14:56:20
如果要将谓词指定为函数参数,则必须为std::plus
提供一个类型,如在此上下文中所示:
some_function(std::plus<T>());
但是,可以直接将其作为模板参数:如下所示:
template < template < class > class PRED, class T >
T foo(T a,T b)
{
return PRED<T>()(a,b);
}
int main()
{
int a = 1;
int b = 1;
foo<std::plus>(a,b);
return 0;
}
就你的情况而言,我认为这是可行的:
template <template <typename> typename Pred, typename ExPolicy, typename InIter, typename T >
typename detail::algorithm_result<ExPolicy, T>::type reduce_seq(ExPolicy const&, InIter first, InIter last, T && init)
{
stuff = Pred<T>()(some_value,other_value);
}
//usage:
reduce_seq<std::plus>(arguments...);
https://stackoverflow.com/questions/24084471
复制相似问题