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

  • Home
  • SEARCH
  • 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 6704631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:18:39+00:00 2026-05-26T07:18:39+00:00

I was wondering if in C++ it was possible to get a function taking

  • 0

I was wondering if in C++ it was possible to get a function taking (n-1) arguments out of a function taking n arguments by setting the value for the nth argument to some value (to be determined at runtime)?

E.g. I would like to do something like the following:

float foo(float a, float b, float c){
  // do some stuff
}

float bar(float x, float y, float f(float q, float r)){
  return f(x,y);
}

int main(){
  double (*function)(float, float);
  float some_value;
  // somehow determine some_value, e.g. read from stdin.
  // Now I would like to set function something along the lines of:
  function = foo( . , . , some_value)
  return bar(123, 456, function);
}

Conceptually, foo is a family of functions indexed by c, i.e. a family of functions foo_c (float a, float b) (read “foo_c” as “foo subscript c” here), and I want to pass foo_c respectively a pointer to it as an argument to bar.

Is this possible at all in C++?
To the best of my knowledge, this isn’t possible with function pointers as above. Is there any other way of doing something like this?

I briefly considered making foo a function template, but this won’t work as I want to set it’s third argument at runtime.

The obvious solution is of course to amend bar so as to take type float f(float q, float r, float t) as its third argument, but this would make the code a lot less reusable (and seems less elegant). I will want to pass other functions to bar as well, some of which will only take two arguments, more than three, or arguments of different types, which I somehow need to set to a fixed value before passing them to bar.

I haven’t quite been able to figure out if I could make bar a template function in this case, along the lines of template <typename T, typename S> float bar(float x, float y, T f(float a, float b, S param), S parameters), and then call it using something like bar <double *(float, float, float), float> (123, 456, function, parameters). Would anything along these lines work at all?

  • 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-26T07:18:39+00:00Added an answer on May 26, 2026 at 7:18 am

    You cannot do it with function pointers, however, it can be done with function objects. I recommend you look at Boost.Bind if you’re using C++03. If you’re using C++11, std::bind is part of the standard and works the same way. C++11 also introduces built-in lambdas which do what you’re attempting.

    For storing the result of bind or a lambda, you can use Boost.Function in C++03 or std::function in C++11.

    This example is how you would do it in C++03.

    float foo(float a, float b, float c){
      // do some stuff
    }
    
    float bar(float x, float y, boost::function<float(float,float)> f){
       return f(x,y);
    }
    
    int main(){
      boost::function<float(float,float)> func;
      float some_value;
      // somehow determine some_value, e.g. read from stdin.
      // Now I would like to set function something along the lines of:
      func= boost::bind(foo, _1, _2, some_value);
      return bar(123, 456, func);
    }
    

    In C++11, you could use the standardized versions of bind and function, or you could use the lambda feature:

    float foo(float a, float b, float c){
      // do some stuff
    }
    
    float bar(float x, float y, std::function<float(float,float)> f){
       return f(x,y);
    }
    
    int main(){
      std::function<float(float,float)> func;
      float some_value;
      // somehow determine some_value, e.g. read from stdin.
      // Now I would like to set function something along the lines of:
      func= [some_value](float x, float y) { return foo(x,y,some_value); };
      return bar(123, 456, func);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i was wondering what would be some of possible ways to get my
I was wondering if it would be possible to use jQuery's ajax function to
I'm wondering if it's possible to get a handle on running instances of a
I was wondering if it is possible to get the places of interest in
I'm wondering if it is at all possible to get the total duration of
Hey all, I'm just wondering if it is possible to get the name of
I was wondering if there was any possible way to get the number (count)
I was wondering if it is possible in a mysql stored-function or stored-procedure compose
I'm wondering if it's possible to get this working: product.PrimaryImage = db.ProductImages .Where(p =>
I was wondering if it is possible to store function variables in memcached. I

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.