News for hackers. Stuff that matters.
Re:Metaprogramming custom control structures in C (2012) by naasking on Sunday July 11, 2021 @11:02AM (#27801370)
Neat, I actually prototyped something like this a few years ago to see how closely I could reproduce parametric polymorphism in C using macros, and it ends up looking a lot like Ada generic packages. The only difference from your style is that I have a "clever" hack to support generic-functions as well and the error messages preserve those names, so your example from github would look something like this:

    #include <stdio.h>

    #define P
    #define T int
    #include <vec.h>

    int compare(int* a, int* b) { return *b < *a; }

    int main(void)
    {
        vec(int) a = vec_init(int)();
        vec_push_back(int)(&a, 9);
        vec_push_back(int)(&a, 1);
        vec_push_back(int)(&a, 8);
        vec_push_back(int)(&a, 3);
        vec_push_back(int)(&a, 4);
        vec_sort(int)(&a, compare);
        foreach(vec(int), &a, it)
            printf("%d\n", *it.ref);
        vec_free(int)(&a);
    }
I really should finish that writeup some day.

There are no comments on this comment yet.