将复杂函数变体作为参数传递
假设我有以下模板函数:
template <class T>
void apply(const vector<complex<T> >& in, vector<T>& out, T (*f)(complex<T>))
{
out.resize(in.size());
for(size_t i = 0; i < in.size(); ++i) out[i] = f(in[i]);
}
您可以看到,我只想将函数应用于复杂数据的向量,并将结果存储到真实数据的向量中。我认为这应该适合整个函数列表:abs、norm、real、imag 等。
我的问题是,如何传入函数?
我尝试过 apply(in, out, abs)
的变体为 abs
提供不同的模板,但没有成功。我很确定问题源于复杂的所有模板的功能,但我不确定如何正确传递它。感谢您的帮助。
Say I have the following template function:
template <class T>
void apply(const vector<complex<T> >& in, vector<T>& out, T (*f)(complex<T>))
{
out.resize(in.size());
for(size_t i = 0; i < in.size(); ++i) out[i] = f(in[i]);
}
You can see, I just want to apply a function to a vector of complex data, and store the results into a vector of real data. I figure this should be good for a whole list of function: abs, norm, real, imag, etc.
My problem is, how do I pass a function in?
I have tried variants of apply(in, out, abs)
supplying different templates to abs
with no luck. I am pretty sure the problem stems from the functions for complex all being templates, but I am not sure how to pass it properly. Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
std::abs
(来自
)将std::complex
参数作为参考 -至常量。您的函数指针仅按值表示,这会导致不匹配。以下代码编译得很好:Ideone 上的实时示例。
然而,更好的想法是简单地采用该函数作为模板参数输入:
Ideone 上的实时示例。
在任何情况下,有时您可能需要在调用站点消除歧义与一个如果函数是模板化的并且重载的(我不记得
函数中的任何一个,但你永远不知道)。The problem is that
std::abs
(from<complex>
) takes thestd::complex<T>
parameter as a reference-to-const. Your function pointer only says by value, which causes the mismatch. The following code compiles just fine:Live example on Ideone.
A better idea, however, would be to simply take the function type as a template parameter:
Live example on Ideone.
In any case, you sometimes might need to disambiguate at the call site with a cast, if a function is templated and overloaded (I don't remember one off-hand from the
<complex>
functions, but you never know).据我所知,您甚至不需要发明
apply
,因为您想要的可以通过std::transform
完成:As far as I can tell, you do not even need to invent
apply
, as what you want can be done withstd::transform
: