Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7218689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:30:59+00:00 2026-05-28T21:30:59+00:00

Say I have the following template function: template <class T> void apply(const vector<complex<T> >&

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:31:00+00:00Added an answer on May 28, 2026 at 9:31 pm

    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>);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following snippet: template <class T> void f(T arg) {
When I try and compile the following code... #include <vector> template <class T> void
let's say I have the following code in A.cpp file: template <typename T> class
Say, I have the following template snippet: <div class=endless_page_template> {% include page_template %} </div>
Say I have the following class: template<class T> struct A { static int value;
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is
Say I have the following class MyComponent : IMyComponent { public MyComponent(int start_at) {...}
I have the following setup: A templated class SpecialModel: template<typename A, typename B> class
Suppose I have the following: #include <iostream> #include <string> template<class T> class base {
say I have the following list that I provide to a django template stuff=

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.