
The Ugliest C Feature: tgmath.h - bcaa7f3a8bbc
https://web.archive.org/web/20131205042841/http://carolina.mff.cuni.cz/~trmac/blog/2005/the-ugliest-c-feature-tgmathh/
======
s_gourichon
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.

A similar trick in C++ is explained on
[https://www.artima.com/cppsource/foreach2.html](https://www.artima.com/cppsource/foreach2.html)
and allows to return the type of an expression _without evaluating it_.

From the link:

> You may be thinking this is needlessly convoluted. After all, couldn'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 "dead code"—it will never
> execute—yet it extends a ghostly finger into the land of the living and
> exerts its influence on the conditional expression's type. Spooky!

~~~
saagarjha
Qt apparently does something similar to implement Q_FOREACH:
[https://stackoverflow.com/a/10522384/5230900](https://stackoverflow.com/a/10522384/5230900)

~~~
likeliv
That was pre-c++11. Now using C++11, it is much simpler (using 'auto'). And
the C++17 version is even simpler

[https://code.woboq.org/qt5/qtbase/src/corelib/global/qglobal...](https://code.woboq.org/qt5/qtbase/src/corelib/global/qglobal.h.html#1085-1107)

------
RcouF1uZ4gsC
With apologies to Philip Greenspun:

Any sufficiently complicated C program contains an ad-hoc, informally-
specified, bug-ridden, slow implementation of half of C++.

~~~
wayoutthere
Sure, but the C++ STL is to code as this [1] is to knives.

[1] [https://www.amazon.com/Wenger-16999-Swiss-Knife-
Giant/dp/B00...](https://www.amazon.com/Wenger-16999-Swiss-Knife-
Giant/dp/B001DZTJRQ).

~~~
benj111
For the low low price of $9999.99.

Is that the maximum you can charge on Amazon? Seems strange that they'd want
to put a limit on it.

------
amelius
I always thought it was setjmp.

[https://en.wikipedia.org/wiki/Setjmp.h](https://en.wikipedia.org/wiki/Setjmp.h)

~~~
saagarjha
setjmp isn’t really that ugly, IMO.

~~~
ludamad
Yeah, low level and rare isn't the same as "bizarrely ripped from the design
of another language"

------
The_rationalist
C11 obscoleted this with the _Generic macro

~~~
EdSchouten
C11 doesn't obsolete it, right? It only permits it to be implemented portably.

For the curious, here's an implementation of tgmath.h on top of _Generic:

[https://github.com/NuxiNL/cloudlibc/blob/master/src/include/...](https://github.com/NuxiNL/cloudlibc/blob/master/src/include/tgmath.h)

~~~
kzrdude
Far from obsolete.. more is coming :)

'Add type generic interfaces for string functions to the C library, see N2068'
has been mentioned as a possible C2x addition
[https://gustedt.wordpress.com/2018/11/12/c2x/](https://gustedt.wordpress.com/2018/11/12/c2x/)

------
jwilk
(2005)

Discussed in 2011:
[https://news.ycombinator.com/item?id=3392728](https://news.ycombinator.com/item?id=3392728)

------
techslave
Has C jumped the shark?

------
likeliv
... and this is why I will never touch C++ and stay with simple C.

(Ah no..., wrong thread. This is actually about C. /s)

