Stupid Constexpr Lambda Trick
Hi All, In the last few months constexpr lambdas and their absence in the standard came up multiple times in boost-devel, expecially when discussing the Hana library. I just wanted to share a C++14 emulation of the feature. The emulation uses both the already known conditional operator trick to allow constepr function objects to be initialized from lambda expressions, plus what I think is a novel trick to allow the actual function object operator() to be constexpr. Implementation + example is here: https://github.com/gpderetta/libtask/blob/master/lambda.cpp Sorry about the macro obfuscation, I was experimenting with a 'cute' lambda interface, but the underlying idea is very simple and pretty obvious in retrospect. I haven't tested that forwarding is always correc and I probably got some cases wrong. Lambda captures and multiple statemens are doable, but are left as an exercise for the reader :) I came up with the trick when trying to implement an emulation of N3617 (Lifting overload sets into function objects). Turns out that implementing the 'quote' syntax is almost trivial in C++14 with generic lambdas, but making it constexpr reliably has so far eluded me as all implementations I have tried ICE both gcc and clang. Still the constexpr lambda emulation itself seems to work. HTH, -- gpd
I just wanted to share a C++14 emulation of the feature. The emulation uses both the already known conditional operator trick to allow constepr function objects to be initialized from lambda expressions, plus what I think is a novel trick to allow the actual function object operator() to be constexpr.
Implementation + example is here:
That looks pretty awesome. Of course, the body of the lambda has to be defined inside of a macro. Either way, this could also add extra weight to persuade the committee to consider adding constexpr lambdas to the standard, since compilers can already do it with a little preprocessor work.
I came up with the trick when trying to implement an emulation of N3617 (Lifting overload sets into function objects). Turns out that implementing the 'quote' syntax is almost trivial in C++14 with generic lambdas, but making it constexpr reliably has so far eluded me as all implementations I have tried ICE both gcc and clang. Still the constexpr lambda emulation itself seems to work.
So it ICEs when doing constexpr lambda emulation to lift overloads? Paul -- View this message in context: http://boost.2283326.n4.nabble.com/Stupid-Constexpr-Lambda-Trick-tp4677494p4... Sent from the Boost - Dev mailing list archive at Nabble.com.
Hi Paul,
for some reason gmail insists in putting all your emails in the spam folder...
On Wed, Jun 24, 2015 at 3:55 AM, Paul Fultz II
I came up with the trick when trying to implement an emulation of N3617 (Lifting overload sets into function objects). Turns out that implementing the 'quote' syntax is almost trivial in C++14 with generic lambdas, but making it constexpr reliably has so far eluded me as all implementations I have tried ICE both gcc and clang. Still the constexpr lambda emulation itself seems to work.
So it ICEs when doing constexpr lambda emulation to lift overloads?
yes. The non-constexpr implementation works fine. But to enable constexpr, even with the trick above, you need to jump to a few levels of nested lambdas to handle SFINAE and both GCC 5 nor clang 3.5 ICE or fail to compile in all variation I have tried. I need to try a more recent version of clang. -- gpd
participants (2)
-
Giovanni Piero Deretta
-
Paul Fultz II