3

I want to create a function that returns a function that checks whether a given int is within certain bounds. Therefore the returned function should only take one parameter, an int, and return a bool. This is necessary as the returned function is passed on as a function pointer to another function. So far I would only be able to do it like this:

bool valueIsInBounds(int value) { return value >= 0 && value <= 100; }

int main() {
    functionThatTakesAFunction(&valueIsInBounds);
}

0 and 100 are obviously fixed values, and I would like to change that.
What I would like to be able to do is something like this:

??? functionGenerator(int min, int max) { ??? }

int main() {
    functionThatTakesAFunction(&(functionGenerator(0, 100)));
}

I know this is doable in other languages, though I don't know how this would be achieved in C.

12
  • It isn't doable in C. Functions can't be dynamically created at runtime. Commented Dec 19, 2016 at 12:55
  • Barring some elaborate machinery to implement first-class functors before writing this on top of it, you can't do that in C. Commented Dec 19, 2016 at 12:55
  • 2
    shameless plug for C++ lambdas Commented Dec 19, 2016 at 12:57
  • 2
    @VittorioRomeo how is a C++ functionality at all relevant to a C question ? Commented Dec 19, 2016 at 12:58
  • 1
    @Quentin As a comment it is definitely relevant. If OP has a very strong reason for wanting to do this and already has a code base in C, converting it to C++ to take advantage of such things would be one possible strategy. Commented Dec 19, 2016 at 13:05

2 Answers 2

4

As dynamically generating a function cannot be done in C, the next best approach will be to parametrize the function.

You can pass (as in your question) the bounds as additional parameters, or you can set global or static variables with these bounds.

bool valueIsInBounds(int value, int min, int max) { return value >= min && value <= max; }

int main() {
    functionThatTakesAFunction(valueIsInBounds);
}

or:

int gMin, gMax;
void setBounds(int min, int max) { gMin= min; gMax= max; }
bool valueIsInBounds(int value)  { return value >= gMin && value <= gMax; }

int main() {
    setBounds(0, 100);
    functionThatTakesAFunction(valueIsInBounds);
}
Sign up to request clarification or add additional context in comments.

Comments

4

Sorry. As the comments says, this can not be implemented in portable C.

You must generate some assembly yourself to bind the parameters (max & min) to the function pointer. This is called a «trampoline» function, and there exists multiple libraries for that. Here is one: https://www.gnu.org/software/libffcall/trampoline.html

Take a look at this SO post for an better explanation and an example of using the trampoline library above.

Comments

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.