6 ms·
Impressive work. I think you can use it twice in a file including it two times with redefine. Can you provide an example with typedef and two different list ty
by daitangio 6y ago
Impressive work. I think you can use it twice in a file including it two times with redefine.
Can you provide an example with typedef and two different list types?
- glouwbug 6y agoI 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); }
- eqvinox 6y agoWait ... "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"?)
- gpderetta 6y agoit seems that it "instantiates" two list types: lst_charp and lst_point and uses both of them. What's the issue?
- deleted 6y ago[deleted]
- wffurr 6y ago“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?
- glouwbug 6y agoYes, as many templates as you like. An A* pathfinder, for instance: https://github.com/glouw/ctl/blob/master/examples/astar.c#L84 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>