Created
June 29, 2016 13:17
-
-
Save aprell/ee98ffc74c8d19bdb9debdd9ca760600 to your computer and use it in GitHub Desktop.
Nested functions in GNU C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <assert.h> | |
| #define defer_(x) \ | |
| ({ typeof(x) fun(void) { return x; } fun; }) | |
| #define defer(n, x) \ | |
| typeof(x) (*n)() = defer_(x) | |
| int main(void) | |
| { | |
| int a = 42; | |
| defer (t1, printf("World\n")); | |
| defer (t2, 1+2); | |
| defer (t3, a++); | |
| printf("Hello "); | |
| t1(); | |
| assert(t2() == 3); | |
| assert((t3(), a) == 43); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment