c*******h 发帖数: 1096 | 1 就是部分的arguments可以动态定死,剩下的才是真正的argument。就像
y = 100;
Afun = @(x)f(x,y);
g(Afun);
在C里面怎样定义Afun? | t****t 发帖数: 6806 | 2 there's no built-in support for this in C. You can achieve the similar
result with c++ std::bind, e.g.
auto afun = std::bind(f, std::placeholders::_1, y);
afun(x);
【在 c*******h 的大作中提到】 : 就是部分的arguments可以动态定死,剩下的才是真正的argument。就像 : y = 100; : Afun = @(x)f(x,y); : g(Afun); : 在C里面怎样定义Afun?
| c*******h 发帖数: 1096 | 3 刚google了一下,可以这样搞
#include
int f(int x, int y) {
return x+y;
}
int g(int (*Afun)(int x), int x) {
return Afun(x);
}
int main(void) {
int y = 100;
int (*Afun)(int);
Afun = ({ int $(int x){ return f(x,y); } $; });
int x = 20;
int z = g(Afun, x);
printf("z = %d\n", z);
}
gcc是work的,不过我换到intel的compiler就不work了
【在 t****t 的大作中提到】 : there's no built-in support for this in C. You can achieve the similar : result with c++ std::bind, e.g. : auto afun = std::bind(f, std::placeholders::_1, y); : afun(x);
| k**********g 发帖数: 989 | 4
The key observation is that it needs "state", i.e. it needs to remember both
a function pointer, as well as a place to store the value of "y".
This is the first step toward understanding object-oriented programming:
variables can be captured into a structure, so that they can be accessed by
functions.
【在 c*******h 的大作中提到】 : 刚google了一下,可以这样搞 : #include : int f(int x, int y) { : return x+y; : } : int g(int (*Afun)(int x), int x) { : return Afun(x); : } : int main(void) { : int y = 100;
| m*******l 发帖数: 12782 | 5 functor
both
by
【在 k**********g 的大作中提到】 : : The key observation is that it needs "state", i.e. it needs to remember both : a function pointer, as well as a place to store the value of "y". : This is the first step toward understanding object-oriented programming: : variables can be captured into a structure, so that they can be accessed by : functions.
| m*******l 发帖数: 12782 | 6 curry --- Functional Language lover
【在 m*******l 的大作中提到】 : functor : : both : by
|
|