<- Back
Comments (81)
- PanzerschrekI don't understand people reinventing macro-based hacks in C to achieve what was achieved in other languages many years ago. Why not using C++, for example? It's available almost everywhere, introducing its usage in an existing C codebase is pretty simple. Sure, C++ has its own downsides, but is it better to create mess with macros in C rather then using exiting language facilities and standard library containers provided by C++?
- lor_louisI wrote a summarizing article on type safe container types a while back, but with some C23 specific changes and a few tweaks to work better for complex types.https://louissven.xyz/article/how_I_do_container_types_in_C....Feel free to flag/delete if this isn't the place.
- tniemiThat `(1 ? (item) : (list)->payload)` was a neat trick. It gets optimized away, but the type comparison happens before that.Still feels a bit like a party trick, but if it works...
- mkehrtI absolutely cannot remember the details, but we did typesafe, macro based generic data structures in C in an undergrad class I took ~20 years ago (CMU Operating Systems). The idea has definitely been around, though I don't know if the implantation was the same.
- veexx103Is it feasible to create a new language by adding features to Cjust as was done with C++?
- randomNumber7As far as I know many C programmers think the addition of variable length arrays in C99 was a mistake. What would be the downside of this approach?
- rramadassAlso see Templates in C by David Priver - https://www.davidpriver.com/ctemplates.htmlHe also has other interesting C techniques, namely;Adding reflection to C - https://news.ycombinator.com/item?id=49964525_Generic for Type Reification in C - https://www.davidpriver.com/creification.htmlSee also his C2y interpreter with REPL named "DrC" for the upcoming C29 standard (https://en.wikipedia.org/wiki/C29_(C_standard_revision)) - https://github.com/drpriver/drcPS: I really like his style of writing and presentation; concise and precise without unnecessary fluff and page beautifying.
- carlos256Jesus, I hate those macro hacks so much. Most of the time you only need a single container, so writing an ad-hoc implementation is cleaner than that.
- tankiya[flagged]
- nice_byteI've tried doing this for a few years and it just sucks ass. Just use c++ and templates if you need proper generic data structures. C is a defective language.