3

say I have a C++ function

int foo(int x, int y){
   return x+y ;
}

Is there a way to create a "parameterized" version of this function?

What I mean is that starting from foo() I would like to define function pointers that have y fixed to a specific values, the equivalent of creating the function foo2() like this:

int foo2(int x){
  return foo(x,2);
}

If not with function pointers, which can be an alternative to have a similar behaviour?

0

4 Answers 4

7

You can fix (or curry) function arguments using std::bind.

For example, foo2 could be

auto foo2 = std::bind(foo, std::placeholders::_1, 2);

You could read this as:

A call to foo2 is like a call to foo where the first argument is the first argument to the foo2 call and the second argument is 2.

The could be done with a lambda function:

auto foo2 = [] (int x) { return foo(x, 2); }

See the above in action.

Finally, if you cannot use C++11 then there's the equivalent boost::bind.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. So what I am looking for is called "binding" in programming? Also, I would like to avoid boost libraries. To your knowledge does g++ with -std=c+11 flag support binding?
@LucaCerone: Binding is one term that you can use, but "fixing arguments" and "currying" are better if you are searching for it. g++ supports C++11, so it does support these solutions (I 've added a link to live code).
Thanks Jon, unfortunately my compiler doesn't support C++11 yet. (It's gcc 4.6, c++11 support has been introduced in 4.7.. I'll try to update, but I don't want to risk that my older program have problems...)
@LucaCerone: For C++03 the simplest solution is to use boost::bind, which uses almost exactly the same syntax (you will need to specify the type of the return value -- use boost::function<int(int)> as the type and see here for more info). Alternatively you can use standard C++03 syntax as per tozka's answer, but that works only for simple cases and is a little bit less readable.
0

You can overload the function with

int foo(int x) {
    return foo(x,2);
}

and you will have the same name... or the default argument solution...

Comments

0

Function pointers do not support anything of the sort. In fact, all they do support is pointing to static functions. Don't ever use them for callbacks, use std::function instead or a template.

You can use std::bind or a lambda to achieve this goal. But you won't get out a function pointer, only a function object, which you can use through the proper abstraction (template or std::function).

Comments

0

You can use std::bind for instance:

std::bind2nd (std::ptr_fun (foo), arg2) (arg1) // replace second argument
std::bind1st (std::ptr_fun (foo), arg1) (arg2) // replace/bind first argument

you can also assign it to variable:

auto fun = std::bind1st (std::ptr_fun (foo), 2);
auto fun = std::bind2nd (std::ptr_fun (foo), 2);

or if your compiler does not support C++11

typedef std::binder1st<std::pointer_to_binary_function<int, int, int>> fun_type;
fun_type fun = std::bind1st (std::ptr_fun (foo), 2); // foo (2, arg2);


typedef std::binder2nd<std::pointer_to_binary_function<int, int, int>> fun_type2;
fun_type2 fun = std::bind2nd (std::ptr_fun (foo), 2); // foo (arg1, 2);

and then call it

fun (arg);

1 Comment

thanks @tozka, I find your answer a bit hard to follow, though I could get the general idea..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.