Live data from Hacker News

The Ugliest C Feature: tgmath.h

web.archive.org

1–10 of 24 posts

Re: The Ugliest C Feature: tgmath.h

#3
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 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!

Re: The Ugliest C Feature: tgmath.h

#4

C11 obscoleted this with the _Generic macro

And _Generic is still somewhat ugly: it has the awkward property that all the branches must pass the type checker even if they are not used. This is mostly OK for numerical functions where you get some help from C’s implicit conversions making the checker more lenient, but if you try to apply _Generic in other contexts you soon encounter problems.

Re: The Ugliest C Feature: tgmath.h

#5

C11 obscoleted this with the _Generic macro

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/...

Re: The Ugliest C Feature: tgmath.h

#7

C11 obscoleted this with the _Generic macro

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/...

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/

Re: The Ugliest C Feature: tgmath.h

#8

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 and allows to return the type of an expression without evaluating it . From the link: > You may be thinking this is needlessl…

That's just C++ wishing that it could have a type system closer to Haskell's.

Re: The Ugliest C Feature: tgmath.h

#9

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 and allows to return the type of an expression without evaluating it . From the link: > You may be thinking this is needlessl…

Qt apparently does something similar to implement Q_FOREACH: https://stackoverflow.com/a/10522384/5230900
Post reply on HN