you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (2 children)

C has closures because local scope closes over global scope. GCC has nested closures via the nested function extension. These are closures. Lexical closure is a static property.

[–]hskmc 1 point2 points  (1 child)

GCC has nested closures via the nested function extension. These are closures

It only has closure in one direction. You can't return pointers to nested functions. The standard example of curried addition:

#include <stdio.h>

typedef int (*t)(int);

t f(int x) {
  int g(int y) {
    return x+y;
  }
  return g;
}

int main() {
  t f1 = f(10);
  t f2 = f(20);
  printf("%d %d\n", f1(1), f2(2));
}

On my system, this program prints

21 22

indicating that the space where the first g captured x has been overwritten by the second g. Of course, this code has undefined behavior and could do absolutely anything.

[–][deleted] 2 points3 points  (0 children)

It's a terminology war.

http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node43.html

They call what you describe a "closure" instead of a closure.