TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Implementing Generic Types in C

89 pointsby sirwhinesalot2 months ago

14 comments

jll29about 2 months ago
&gt; The former is nicer to program while the latter is nicer to use.<p>When I have such a situation, I&#x27;m inclined to write myself my own pre-processor (as I did for Pascal once in a previous millenium, on an Atari ST 520+), so that you can write in a style that is nicer to program in, which gets pre-compiled into something that is nicer to use from your client code.<p>Nothing comes without downsides: the price of this is that other developers need to understand your idiosyncratic pre-processor, so this method works best for &quot;single author&quot; code&#x2F;personal projects.<p>What you don&#x27;t want in a team is each coder having their own way of doing things, so that you cannot share libraries and helper functions.<p>BTW, the best book on the OP&#x27;s topic of production coding in C and implementing type-safe ADTs is Chris Hanson&#x27;s book &quot;C: Interfaces and Implementations.&quot; It contains some eye-opening tricks even for experienced developers in (standard) C.
评论 #43417068 未加载
评论 #43419510 未加载
spacedcowboyabout 2 months ago
Probably worth mentioning the C Template Library [1]...<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;glouw&#x2F;ctl&#x2F;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;glouw&#x2F;ctl&#x2F;</a>
lukaslalinskyabout 2 months ago
For C with generic types, I think one should pick up Zig. It&#x27;s exactly that, it&#x27;s a low level language with manual memory management, but it allows you to do generic types like this without hacks.
评论 #43416653 未加载
评论 #43415950 未加载
评论 #43416601 未加载
评论 #43420641 未加载
codr7about 2 months ago
This is how I do proper generic vectors in C, no template tricks needed and no pointer chasing involved:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;codr7&#x2F;hacktical-c&#x2F;tree&#x2F;main&#x2F;vector" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;codr7&#x2F;hacktical-c&#x2F;tree&#x2F;main&#x2F;vector</a><p>The problem was always pretending C is something it&#x27;s not, that never works well in any language.
WalterBrightabout 2 months ago
When moving into generic types and metaprogramming in C, your only real choice is macros. I&#x27;ve been down that path. It just never seems to work out very well, and the results were always unsatisfying.<p>At some point, it becomes worthwhile to graduate to a more powerful language that still retains all the low level capability, like DasBetterC.
dleslieabout 2 months ago
I&#x27;ve never heard of CC before; the ergonomics of it look positively _modern_.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;JacksonAllan&#x2F;CC" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;JacksonAllan&#x2F;CC</a>
dsp_personabout 2 months ago
I&#x27;ve found useful doing an implementation with void* and having thin macros to do casting with typeof() so the usage is type checked.
huhtenbergabout 2 months ago
&gt; <i>I personally quite enjoy programming in “C with methods, templates, namespaces and overloading”, avoiding the more complicated elements of C++ (like move semantics2)</i><p>Don&#x27;t we all.<p>Except for the committee, of course, and its entourage.
tidwallabout 2 months ago
Here&#x27;s a b-tree library I wrote that uses a similar approach. <a href="https:&#x2F;&#x2F;github.com&#x2F;tidwall&#x2F;bgen" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tidwall&#x2F;bgen</a>
attractivechaosabout 2 months ago
For linked lists and binary trees, intrusive data structures are better.<p>&gt; <i>Well, except the first one, template macros, where I can’t really find any pro, only cons.</i><p>For toy examples, the first (expanding a huge macro) has mostly cons. But it is more flexible when you want to instantiate different parts of the header. The second approach can work but will be clumsy in this case because the whole header is considered as one unit.
juancnabout 2 months ago
The Java example doesn&#x27;t really compile due to generic arrays and mixing primitives and non-primitives, but the point still holds.<p>Type erasure is still type checked though, it&#x27;s just lost at runtime (i.e. the generics are not reified).<p>This works for Java very well because the JIT will have another chance at further optimizations once the application runs.
评论 #43416624 未加载
marcodiegoabout 2 months ago
<a href="https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg14&#x2F;www&#x2F;docs&#x2F;n3509.pdf" rel="nofollow">https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg14&#x2F;www&#x2F;docs&#x2F;n3509.pdf</a>
评论 #43419530 未加载
cyber1about 2 months ago
Unfortunately, the standardization committee plays around with macros and _Generic instead of investing their time in modern metaprogramming ideas
wolfspawabout 2 months ago
Lol, thats insane.<p>And awesome, I love it!