Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a complex function variants as arguments

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.

like image 249
steveo225 Avatar asked Mar 25 '26 10:03

steveo225


1 Answers

The problem is that std::abs (from <complex>) takes the std::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:

#include <vector>
#include <complex>

template <class T>
void apply(const std::vector<std::complex<T> >& in, std::vector<T>& out,
           T (*f)(std::complex<T> const&))
{
    out.resize(in.size());
    for(size_t i = 0; i < in.size(); ++i)
      out[i] = f(in[i]);
}

int main(){
  std::vector<std::complex<float> > vcomp;
  std::vector<float> vf;
  apply(vcomp, vf, &std::abs<float>);
}

Live example on Ideone.

A better idea, however, would be to simply take the function type as a template parameter:

template <class T, class F>
void apply(const std::vector<std::complex<T> >& in, std::vector<T>& out, F f)
{
    out.resize(in.size());
    for(size_t i = 0; i < in.size(); ++i)
      out[i] = f(in[i]);
}

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).

// taking std::abs as an example. It's not actually templated *and* overloaded
typedef float (*func_ptr)(std::complex<float> const&);
apply(vcomp, vf, (func_ptr)&std::abs<float>);
like image 198
Xeo Avatar answered Mar 27 '26 23:03

Xeo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!