![](/img/trans.png)
[英]Undefined reference to a custom function in a worker (C++ and RcppParallel)
[英]RcppParallel worker with additional arguments
这是我第一次尝试使用 RcppParallel package,我必须使用C++17 (Ubuntu)
我试图靠近开发人员站点的ParallelFor示例,但我需要一个额外的(非迭代的)参数作为 worker threshold
。
这是我当前的代码
struct ReplaceWorker : public Worker
{
// source matrix
const RMatrix<double> input;
// destination matrix
RMatrix<double> output;
// threshold
double th;
// initialize with source and destination
ReplaceWorker(const NumericMatrix input, NumericMatrix output, double threshold)
: input(input), output(output), th(threshold) {}
// replace function
template<typename T>
double replacer(const T &x){
if(x < th){
return(0);
} else {
return(1);
}
}
// take the square root of the range of elements requested
void operator()(std::size_t begin, std::size_t end) {
std::transform(input.begin() + begin,
input.begin() + end,
output.begin() + begin,
replacer);
}
};
但是我总是以相同的编译错误结束:
usr/include/c++/7/bits/stl_algo.h:4295:5: note: candidate: template<class _IIter, class _OIter, class _UnaryOperation> _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)
transform(_InputIterator __first, _InputIterator __last,
^~~~~~~~~
/usr/include/c++/7/bits/stl_algo.h:4295:5: note: template argument deduction/substitution failed:
network_edge_strength.cpp:173:28: note: couldn't deduce template parameter ‘_UnaryOperation’
replacer);
^
/usr/include/c++/7/bits/stl_algo.h:4332:5: note: candidate: template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)
transform(_InputIterator1 __first1, _InputIterator1 __last1,
^~~~~~~~~
/usr/include/c++/7/bits/stl_algo.h:4332:5: note: template argument deduction/substitution failed:
network_edge_strength.cpp:173:28: note: candidate expects 5 arguments, 4 provided
replacer);
^
任何建议,如何解决此问题或替代方案,如何使其以所需的threshold
参数运行?
replacer
is a function template , not a function, which means it cannot be used as a function object unless a specific instantiation is used, as otherwise template argument deduction fails.
此外,作为成员 function,它需要隐式 object 参数才能调用。
您可以改用通用 lambda 表达式:
std::transform(/* [...] */, [this] (const auto& x) { return replacer(x); });
这样,即使replacer
过载或者是 function 模板,这也可以工作。
或者,完全删除replacer
器,并直接使用 lambda 表达式:
std::transform(/* [...] */, [this] (const auto& x) { return x < th ? 0 : 1; });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.