Hacker News new | past | comments | ask | show | jobs | submit login

I imagine something along the lines of:

    typedef char* charp;
    #define P
    #define T charp
    #include <lst.h>

    typedef struct { int x, y; } point;
    #define P
    #define T point
    #include <lst.h>

    int main(void)
    {
        lst_charp a = lst_charp_init();
        lst_point b = lst_point_init();
        lst_charp_push_back(&a, "the");
        lst_charp_push_back(&a, "quick");
        lst_charp_push_back(&a, "brown");
        lst_charp_push_back(&a, "fox");
        lst_point_push_back(&b, (point) { 53, 32 });
        lst_point_push_back(&b, (point) { 11, 22 });
        lst_point_push_back(&b, (point) { 83, 23 });
        lst_charp_free(&a);
        lst_point_free(&b);
    }



Wait ... "imagine" - you can't currently do more than one? That's kind of the essential point with templates.

(I scrolled through it and assumed you'd just do "#define lst list_charp"?)


“Imagine” as in that code is written in an HN comment, not a C source file, and probably hasn’t been checked with a compiler yet. Why don’t you copy and paste it into a test, try it out, and report back?


Yes, as many templates as you like. An A* pathfinder, for instance: https://github.com/glouw/ctl/blob/master/examples/astar.c#L8...

Although if you're interested in just a batch rename, it's something like this:

    #define vec_char str
    #define P
    #define T char
    #include <vec.h>


it seems that it "instantiates" two list types: lst_charp and lst_point and uses both of them. What's the issue?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: