Last active
June 7, 2021 10:04
-
-
Save bynect/947a048e8dee104087c2efe8e013e2de to your computer and use it in GitHub Desktop.
Anonymous functions in C using GCC extensions.
This file contains 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
#ifndef _LAMBDA_H | |
#define _LAMBDA_H | |
#define LAMBDA(stmt, ret, ... /*args*/) \ | |
({ ret __fun__(__VA_ARGS__) { stmt; }; __fun__; }) | |
#endif |
This file contains 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 "lambda.h" | |
int main() | |
{ | |
void (*fun) (int, int) = LAMBDA( | |
printf("%d + %d == %d", a1, a2, a1 + a2), | |
void, | |
int a1, int a2 | |
); | |
fun(123, 456); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment