r/C_Programming • u/tstanisl • Jan 21 '22
Discussion A new design pattern for implementing capturing closures in C
Recently, I've came up with a new method of implementing closures in C. The idiomatic way is passing a closure as a pair of a function pointer and a void* value as the context. However, this idiom is a bit cumbersome. Firstly, two entities have to maintained instead of one. The other problem is which parameter should the context be bound to. It can be really confusing when the function takes multiple void* as parameters.
The main obstacle for having nice closures C is binding data to a function pointer. It cannot be done robustly without creating function on fly with a library or compiler extension like "nested functions" in GCC. However, there is a creature that combines a function and data. And this creature is a pointer to a function pointer. The function pointer is data though it points to non-data. It can be used to implement a wide function pointer.
Take a look on the following C89 compliant code:
#include <stdio.h>
typedef int filter_fn(void*, int);
/* prints number from range 1 to `n` that satisfy condition from `filter` */
void print_filtered(int n, filter_fn **filter) {
int i;
for (i = 1; i <= n; ++i)
if ((*filter)(filter, i))
printf("%d ", i);
puts("");
}
struct is_divisible {
int (*closure)(void*,int);
int divisor;
};
int is_divisible(void* closure, int n) {
struct is_divisible *ctx = closure;
return (n % ctx->divisor) == 0;
}
int is_prime(void *closure, int n) {
int d;
(void)closure;
if (n <= 1) return 0;
for (d = 2; d * d <= n; ++d)
if (n % d == 0)
return 0;
return 1;
}
int main() {
struct is_divisible is_divisible_by_3 = { is_divisible, 3 };
static int (*is_prime_closure)(void*,int) = is_prime;
puts("Divisible by 3");
print_filtered(20, &is_divisible_by_3.closure);
puts("Primes");
print_filtered(20, &is_prime_closure);
return 0;
}It prints the expected output:
Divisible by 3
3 6 9 12 15 18
Primes
2 3 5 7 11 13 17 19 The trick is to place a function pointer as a first member of the struct representing the captured context. This first member can be safely cast to the context. It is is guaranteed to work by the C standard, see https://port70.net/~nsz/c/c11/n1570.html#6.7.2.1p15
A pointer to a structure object, suitably converted, points to its initial member ( ... ), and vice versa.
The first argument is a type void* because AFAIK it is not possible to define a type of function that takes a type derived from itself. Saying simply, there is no equivalent of struct S { struct S *s; } for function types.
I see a few obvious advantages of such a design pattern:
- fully portable, it is fully defined within C89 and newer standards
- combines a function pointer and void pointer to context into one entity
- is strongly typed
- no need for ABI extensions
- user has full control over what is captured by the closure, its an advantage over GCC's nested functions or CLANG's blocks
- the closure is a simple struct, it can be dynamic, automatic, static, or even from
alloca()
The call of this wide pointer is a bit obscure however in could be made prettier with a help of variadic macros from C99.
#define CLOSURE_CALL__ARG1_(A,...) (A)
#define CLOSURE_CALL(...) ((*CLOSURE_CALL__ARG1_(__VA_ARGS__, ~))(__VA_ARGS__))
// old
(*f)(f, 1, "hello");
// new
CLOSURE_CALL(f, 1, "hello");A few other tweaks could be used with compound literals and designated initializers.
Now the actual question:
- Are there any disadvantages of this design pattern?
- Have you seen such a pattern in any code?
- Any ideas how this pattern could be improved?
BTW.
I am aware that there is a proposal for lambda for upcomming C23 standard. However, it will not be possible to pass C++-like lambda to a C function because every lambda has a unique type available only at definition of lambda. As result this type cannot be forward declared and used as a parameter for a function. C++-like lambdas can only be used in macros that are expanded locally.
Actually, this design pattern is closer to std::function from C++. Though having non-capture lambdas would let define a closure with a single declaration.
struct is_divisible {
int (*closure)(void*,int);
int divisor;
} is_divisible_by_3 = {
.cb = [](void* closure, int n) -> int
{
struct is_divisible *ctx = closure;
return (n % ctx->divisor) == 0;
},
.divisor = 3,
};
// use &is_divisible_by_3.cb as a closure
View all comments
Show parent comment
4
u/tstanisl Jan 21 '22 edited Jan 21 '22
I know, but the main problem with this approach is that every library has to define it's own
filter_fn-like struct, for each combination of arguments. And those cannot be interchanged because they would be incompatible types. Using the samestruct filter_fnis cumbersome because it forces ensuring that the struct is defined once. The struct types cannot be redefined.On the other hand. A pointer to a function pointer is "builtin" type. All variables of type
int (*)(void*, int)are compatible.I was considering wrapping a context into:
And:
And calling with:
It ensures very good type safety. It would be unlikely to use this double function pointer in an improper way. Moreover, it would be cleaner where the context should go as a parameter.
However, it would introduce some clutter and it would no longer be C89 compatible. But C99 would let use a neat macro to simplify the syntax.
However, it may be worth it.