Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I did something similar in a personal project[0], but to avoid reimplementing things like lists and resizable buffers and such over and over again but maintain a sort of "type safety.*" It's a step down from C++ templates but it works.

[0] https://github.com/noobermin/ch



You may or may not have been aware, but most Unixes provide `sys/queue.h`[0] which contains many macros for type safe collections.

0 - http://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/sys/sys/...


Thanks! I actually did not know!


linux has similar files too


Here is my full hash table implementation done in the same manner:

https://github.com/comex/substitute/blob/master/lib/cbit/hta...

It is uglier than C++ templates, no doubt about it[1]. However, it does have two advantages over STL std::unordered_map (not applicable to a custom C++-template implementation):

- It's guaranteed to have low code size overhead for each supported key type, and none for additional specializations with the same type of key but different types of value.

- It is possible to explicitly instantiate said code in one .c file, rather than the standard C++ approach of forcing the compiler to redo code generation for every .cpp file and hoping the linker merges duplicates. This is mainly an advantage for compile time. C++11 has extern template, but you can't use it with the STL.

(It's also easier to do 'raw' things with the table, although this is just an implementation choice with upsides and downsides rather than a flaw in the STL. And independently from the duplicate specialization, C just compiles faster than C++, and I am fanatic about compile time.)

[1] At least, C++ templates implemented sanely. Certain STL implementations (GNU libstdc++) appear to have a goal of being as difficult to read as possible.




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

Search: