Function Index with Preset Parameters

How can I create a pointer to a function in which some parameters are set for definition by definition.

Here is an example of what I mean:

Say I have a function

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

and type of function pointer

typedef int (*increaser)(int);

What I want is a pointer to a function addthat fixes the first parameter to 1 and leaves the second open paragraph. Something along the lines

increaser f = &add(1,x);

How can i do this?

+4
source share
2 answers

What I want is a pointer to the add function, which captures the first parameter to 1 and leaves the second open paragraph open.

C. . - :

int add1(int x) {
    return add(1, x);
}

increaser f = &add1;

, :

#define increaser(x) add(1, (x))
+3

C . ++ std:: function bind, .

, C- , add:

int increment(int input) {
    return add(1, input);
}

:

increaser f = &increment;
+1

Source: https://habr.com/ru/post/1655853/


All Articles