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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Templating in C

78 点作者 ux超过 10 年前

11 条评论

dan-olivier超过 10 年前
&quot;Nevertheless, this is still a few order of magnitude better than C++ templating.&quot;<p>Uh... no. On a personal level, I have always found that almost any type of preprocessor magic, especially re-#including with a different set of parameters in effect is a recipe for disaster.<p>Any use of the pre-processor seems to have a serious impact on the build-system (tracking parallel variants), the IDE (color, intellisense or auto-completion), the debugger, and apparently any tool that operates at the meta-level.<p>On the other hand, IDEs and debuggers often also have problems with C++ templates as well. Am I crazy to think that the result has generally been better with templates?<p>I think we would better off without any of this. We should: - discard the preprocessor, - expanding and tightening control on templating), and - migrating towards more conventional module import methods, such as <a href="http://clang.llvm.org/docs/Modules.html" rel="nofollow">http:&#x2F;&#x2F;clang.llvm.org&#x2F;docs&#x2F;Modules.html</a> (instead of #include, unless you can fix IWYU).
评论 #9124648 未加载
评论 #9124492 未加载
silentvoice超过 10 年前
Curious it doesn&#x27;t mention C11&#x27;s __Generic macro. A very nice way to achieve a lot of useful &quot;overloading&quot; behavior.<p><a href="http://en.wikipedia.org/wiki/C11_%28C_standard_revision%29" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;C11_%28C_standard_revision%29</a>
评论 #9125080 未加载
评论 #9125299 未加载
bitwize超过 10 年前
Just use C++. The lack of semantically aware, <i>type safe</i> templates is one of the deficiencies of C that motivated the development of C++.<p>C++ is a <i>strictly more powerful language</i> than C. Play to its strengths rather than abusing C&#x27;s weaker constructs.
评论 #9125594 未加载
评论 #9126581 未加载
评论 #9125525 未加载
评论 #9125754 未加载
mbel超过 10 年前
Another solution, which seems to be missing is to use function pointers; process_image could receive a pointer to given function instead of n. My experiences with gcc show, that functions passed by pointer are often inlined, which is not really a surprise, since they are constant arguments of the function.<p>Here is an example [0], although the wrapper functions seem to be redundant in this case.<p>[0] <a href="http://pastebin.com/BAgm61S8" rel="nofollow">http:&#x2F;&#x2F;pastebin.com&#x2F;BAgm61S8</a>
评论 #9124983 未加载
评论 #9125103 未加载
评论 #9124485 未加载
arquivo超过 10 年前
Why complicate with multiple files, functions, defines and includes; here it is in one file with a single define:<p><a href="http://pastebin.com/uWSs7ca7" rel="nofollow">http:&#x2F;&#x2F;pastebin.com&#x2F;uWSs7ca7</a><p>We can improve the macro further by removing the unnecessary if statement and the index number:<p><a href="http://pastebin.com/vPTTYPpe" rel="nofollow">http:&#x2F;&#x2F;pastebin.com&#x2F;vPTTYPpe</a>
评论 #9125094 未加载
评论 #9124741 未加载
noobermin超过 10 年前
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 &quot;type safety.*&quot; It&#x27;s a step down from C++ templates but it works.<p>[0] <a href="https://github.com/noobermin/ch" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;noobermin&#x2F;ch</a>
评论 #9124723 未加载
评论 #9125068 未加载
motoki88超过 10 年前
I am looking for a book or web page which describes well programming patterns in C (might be focused on OOP in C). I mostly design for embedded systems. Any recomendations?
评论 #9124504 未加载
评论 #9124543 未加载
halayli超过 10 年前
here&#x27;s a similar technique I use in lthread to generate functions:<p><a href="https://github.com/halayli/lthread/blob/master/src/lthread_socket.c#L311-L365" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;halayli&#x2F;lthread&#x2F;blob&#x2F;master&#x2F;src&#x2F;lthread_s...</a><p>the arguments are complete function signature
kyberias超过 10 年前
The most interesting code is dark grey text on black background.
_RPM超过 10 年前
C macros are powerful.
yannsionneau超过 10 年前
Very interesting!