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.

Advanced Metaprogramming in C: A select statement

58 pointsby goranmoominalmost 3 years ago

7 comments

mckirkalmost 3 years ago
On the one hand, I personally enjoy exercises like this, and the end result is quite pleasing to look at. On the other hand, the whole idea is kind of funny to me.<p>In my experience, a proclivity to programming serious low-level code in C very much comes with an intense defensive reaction to things like &quot;metaprogramming using the C precompiler&quot;. At least I still vividly the remember my co-workers&#x27; reaction to the C code I wrote as an intern, when I tried to be clever.
评论 #32430581 未加载
评论 #32430488 未加载
quelsolaaralmost 3 years ago
I have thought a lot about meta programming and the best way to do it (I participate in the ISO C wg14) and I dislike macros, templets and every other attempt I have seen.<p>The best solution I have found, disregarding of language, is to use the same language to write a program that generates the code you need.<p>Then you can compile, and debug them separately using the same workflow, and each program can be clear and simple to read. Its not sexy or clever, but it works and anyone can understand what is going on. Almost always that turns out to be the most important thing.
评论 #32433033 未加载
评论 #32431480 未加载
评论 #32439143 未加载
rrdharanalmost 3 years ago
<a href="http:&#x2F;&#x2F;libmill.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;libmill.org&#x2F;</a> “is in an active auction”.<p><a href="https:&#x2F;&#x2F;www.sav.com&#x2F;auctions&#x2F;details&#x2F;1159199&#x2F;libmill.org" rel="nofollow">https:&#x2F;&#x2F;www.sav.com&#x2F;auctions&#x2F;details&#x2F;1159199&#x2F;libmill.org</a><p>Insert some joke about the dangers of overly abusing the C precompiler…
评论 #32429340 未加载
jstimpflealmost 3 years ago
Metaprogramming should be limited, and metaprogramming in C is quite brittle and almost nobody really understand the execution model of he preprocessor. But, dismissing the preprocessor outright is too quick a conclusion. There are some clever and very practical uses of the preprocessor.<p>- portability<p>- generate strings from identifiers<p>- Insert file and line of the call location (or other context) automatically when calling a function.<p>- X-macros<p>- Scopes with exit statement given at beginning, hacked using &quot;for&quot; statement and a condition that evaluates to true only the first time. A little bit brittle when the exit statement must not be skipped (goto, return).<p>I&#x27;m sure there are more, but I&#x27;m too tired to think harder... Here is an example of the last category:<p><pre><code> #define TIMED_SECTION_(name, t) FOR_SCOPE(Timer t = start_timer(); print_passed_time(name, t)) #define TIMED_SECTION(name) TIMED_SECTION_(name, UNIQUE_NAME()) </code></pre> Not giving the FOR_SCOPE() and UNIQUE_NAME() macros here for brevity. They add another 3-5 reusable lines. Use like this:<p><pre><code> TIMED_SECTION(&quot;foo the bar&quot;) { &#x2F;* Do stuff, time spent in this scope will be measured and printed on scope exit. *&#x2F; } </code></pre> Other more advanced languages can do some of the same things, sometimes more robustly, or not at all (X-macros is probably hard to replace for languages that are not LISP). But it typically comes at a cost in complexity, hard to use type systems, complicated semantics with edge cases... And they only allow you to do what is already built into the language!
评论 #32435239 未加载
WalterBrightalmost 3 years ago
If you&#x27;re using macros for metaprogramming in C, you&#x27;ve outgrown the language and are ready for a more powerful C replacement.
评论 #32432763 未加载
评论 #32437789 未加载
评论 #32437749 未加载
tpoacheralmost 3 years ago
&gt; However, such syntax is ugly and verbose.<p>Is it just me who thinks this was by far the clearest, cleanest formulation? Everything following it seemed to sacrifice clarity for the sake of cleverness.<p>Also I really dislike macros that try to disguise themselves as &quot;not macros&quot;. Capitalise the crap out if it, make the fact that this is a macro stand out, man!
jxyalmost 3 years ago
Using initializer list would be much more readable without the atrocious macros.
评论 #32430675 未加载