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.

The Ugliest C Feature: tgmath.h

107 pointsby bcaa7f3a8bbcover 5 years ago

7 comments

s_gourichonover 5 years ago
The ternary operator is indeed special as it (is the only one AFAIK that) introduces in the language some type constraints between the two result branches, that affect the way one or the other is interpreted.<p>A similar trick in C++ is explained on <a href="https:&#x2F;&#x2F;www.artima.com&#x2F;cppsource&#x2F;foreach2.html" rel="nofollow">https:&#x2F;&#x2F;www.artima.com&#x2F;cppsource&#x2F;foreach2.html</a> and allows to return the type of an expression <i>without evaluating it</i>.<p>From the link:<p>&gt; You may be thinking this is needlessly convoluted. After all, couldn&#x27;t we just use encode_type(container) without the conditional operator and get the same result? No, because that would have caused container to be evaluated. With the conditional operator, only one branch of the condition is ever executed. In this case, since the condition is always true, the first branch will always be taken. The second branch is &quot;dead code&quot;—it will never execute—yet it extends a ghostly finger into the land of the living and exerts its influence on the conditional expression&#x27;s type. Spooky!
评论 #21240092 未加载
评论 #21242248 未加载
评论 #21240927 未加载
评论 #21240051 未加载
RcouF1uZ4gsCover 5 years ago
With apologies to Philip Greenspun:<p>Any sufficiently complicated C program contains an ad-hoc, informally-specified, bug-ridden, slow implementation of half of C++.
评论 #21242932 未加载
评论 #21241328 未加载
评论 #21241474 未加载
ameliusover 5 years ago
I always thought it was setjmp.<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Setjmp.h" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Setjmp.h</a>
评论 #21240120 未加载
The_rationalistover 5 years ago
C11 obscoleted this with the _Generic macro
评论 #21239947 未加载
评论 #21239959 未加载
jwilkover 5 years ago
(2005)<p>Discussed in 2011: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=3392728" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=3392728</a>
techslaveover 5 years ago
Has C jumped the shark?
likelivover 5 years ago
... and this is why I will never touch C++ and stay with simple C.<p>(Ah no..., wrong thread. This is actually about C. &#x2F;s)