Re:Yet Another TypeSafe and Generic Programming Candidate for C
by brightprogramer on Wednesday September 10, 2025 @02:48AM
(#45194149)
Yea the FMT trick uses VA_OPT. I consider myself a noob with C official specification, so you're probably right about that.
I do kinda like how foreach is implemented right now. This allows me to perform some strict checking for easy loop iteration based bugs and also it kinda looks cool.
This is the first time I came across typeof_unqual. I'll look into it, thanks .
I spent a lot of time making this work across all three compilers (especially MSVC). I'm glad MSVC has VA_OPT support.
Re:Yet Another TypeSafe and Generic Programming Candidate for C
by teo_zero on Wednesday September 10, 2025 @04:29PM
(#45203182)
> I do kinda like how foreach is implemented right now. This allows me to perform some strict checking for easy loop iteration based bugs and also it kinda looks cool.
Yes but the body in the parameters really is a show stopper. What if the body has a unprotected comma? Like
VecForeach(&v, e, {
int x, y;
...
});
Better to expand the macro into one or two "for" and let the body follow. For example: #define VecForeachIdx(v, var, idx)\
for (size idx=0,_d=1; ValidateVec(v),_d; _d--)\
for (VEC_DATATYPE(v) var={}; idx<(v)->length && (var=VecAt(v,idx),1); idx++)
Please note that I haven't tested it.
Re:Yet Another TypeSafe and Generic Programming Candidate for C
by brightprogramer on Wednesday September 10, 2025 @06:14PM
(#45204747)
That sounds like a better idea. I'll test it. Debugging with my existing macros is a PITA, but if your suggestion works then it'll work well with debuggers as well!
Thanks :)
Re:Yet Another TypeSafe and Generic Programming Candidate for C
by servicewelt on Thursday September 11, 2025 @05:16AM
(#45209456)
How would you implement 'var = ...' while respecting that 'VecAt(...)' could return 0?
I am currently working on a similar problem and can't seem do seem figure out how to handle these for-loops and setting the iterator properly.
Re:Yet Another TypeSafe and Generic Programming Candidate for C
by teo_zero on Thursday September 11, 2025 @06:44AM
(#45210000)
The ",1" ignores var and keeps 1.
Re:Yet Another TypeSafe and Generic Programming Candidate for C
by brightprogramer on Saturday September 20, 2025 @01:54PM
(#45315600)
I just tested this, and it works flawlessly! This is amazing! Thanks for the suggestion.
I'd say "mostly C11": it uses __VA_OPT__ that's been standardized only in C23.
The "foreach" macros need a lot of refinement: passing the body in the parameters is asking for troubles, for example. And using a non-unique default name for the index prevents nested loops.
To overcome the issues with generic and qualified types, have you considered using typeof_unqual?