Qt apparently does something similar to implement Q_FOREACH: https://stackoverflow.com/a/10522384/5230900
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).
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.
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!