TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Implementing Generic Types in C

89 点作者 sirwhinesalot2 个月前

14 条评论

jll29大约 2 个月前
&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 未加载
spacedcowboy大约 2 个月前
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>
lukaslalinsky大约 2 个月前
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 未加载
codr7大约 2 个月前
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.
WalterBright大约 2 个月前
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.
dleslie大约 2 个月前
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_person大约 2 个月前
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.
huhtenberg大约 2 个月前
&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.
tidwall大约 2 个月前
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>
attractivechaos大约 2 个月前
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.
juancn大约 2 个月前
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 未加载
marcodiego大约 2 个月前
<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 未加载
cyber1大约 2 个月前
Unfortunately, the standardization committee plays around with macros and _Generic instead of investing their time in modern metaprogramming ideas
wolfspaw大约 2 个月前
Lol, thats insane.<p>And awesome, I love it!