1

How can I include arguments in a function pointer?

This code creates a function pointer that can add two ints:

int addInt(int n, int m) {
    return n+m;
}
int (*functionPtr)(int,int);
functionPtr = addInt;
(*functionPtr)(3,5); // = 8

For instance, I want to make a function pointer where the first argument is always 5, so that the function takes one int and adds five. And another one where the first argument is 8.

Is this possible using addInt? Something like:

// make-believe code that of course won't work 
int (*functionPtr)(int);
functionPtr = addInt(5);
(*functionPtr)(3); // = 8
(*functionPtr)(9); // = 14
4
  • 1
    You can't, not with function pointers anyway. You can however use std::bind to do it. Commented Mar 11, 2015 at 9:22
  • why do you need a function pointer if you are going to add that value to something anyway? BTW there are no default values for functions, only constructors Commented Mar 11, 2015 at 9:22
  • @Pandrei This was a minimal example, not my actual problem Commented Mar 11, 2015 at 9:23
  • @Anna: you probably should have asked about your actual problem, and you certainly should mention if you are coding in C++11, C++14 or some older versions of the C++ standard. Commented Mar 11, 2015 at 9:34

3 Answers 3

4

Use std::bind like this:

using namespace std::placeholders;
auto f = std::bind(addInt, 5, _1);

f(1); //returns 6
Sign up to request clarification or add additional context in comments.

Comments

3

Use std::bind and placeholders

#include <iostream>
#include <functional>

using namespace std;

int addInt(int n, int m) {
    return n+m;
}

int main() {
    int (*functionPtr)(int,int);
    functionPtr = addInt;
    (*functionPtr)(3,5); // = 8
    auto f2 = std::bind( addInt, std::placeholders::_1, 5);
    auto f3 = std::bind( addInt, 8, std::placeholders::_1);
    std::cout << f2(1) << "\n";;
    std::cout << f3(1) << "\n";;
}

Output:
6
9

Comments

3

What you really want is a closure (you might also want curryfication, but C++ don't have that; consider switching to Ocaml if you really want it).

C+14 and C++11 have closures (but not earlier versions of C++). Read about C++ lambda functions (or anonymous functions) and the standard <functional> header and its std::function template.

Here is the function which given some integer d returns the translation by d i.e. the function taking an integer x and returning x+d

#include <functional>
std::function<int(int)> translation(int d) {
  return [=](int x) { return addInt(x,d) /* or x+d */ ; };
}

Notice that std::function-s are not just C function pointers. They also contain closed values (d in my translation example)

The auto and decltype specifiers are tremendously useful. For example:

auto addfive = translation(5);
std::cout << addfive(3) << std::end; // should output 8

2 Comments

I think using auto instead of std::function<int(int)> would be better, at least if you get to assign it where you declare it like that.
@Hunkyl: Are you sure? I would believe that for a newbie expliciting std::function is more readable!

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.