News for hackers. Stuff that matters.
Re:The Ugliest C Feature: tgmath.h by s_gourichon on Sunday October 13, 2019 @08:07AM (#21239944)
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 by saagarjha on Sunday October 13, 2019 @08:49AM (#21240092)
Qt apparently does something similar to implement Q_FOREACH: https://stackoverflow.com/a/10522384/5230900
Re:The Ugliest C Feature: tgmath.h by likeliv on Sunday October 13, 2019 @12:17PM (#21241182)
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...

Re:The Ugliest C Feature: tgmath.h by MauranKilom on Sunday October 13, 2019 @03:12PM (#21242248)
For those wondering (like I did)

"But there already is a C++ facility whose sole purpose is determining the type of an expression [or entity] without evaluation it?"

decltype() only exists since C++11 (and the above article is from 2006).

https://en.cppreference.com/w/cpp/language/decltype

Re:The Ugliest C Feature: tgmath.h by aardvark179 on Sunday October 13, 2019 @11:34AM (#21240927)
That sort of operator is often tough, just look at section 15.25 in the Java language specification (https://docs.oracle.com/javase/specs/jls/se13/html/jls-15.ht...) in the type of such expressions.
Re:The Ugliest C Feature: tgmath.h by smitty1e on Sunday October 13, 2019 @08:34AM (#21240051)
That's just C++ wishing that it could have a type system closer to Haskell's.