|
Boost : |
Subject: [boost] Stupid Constexpr Lambda Trick
From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2015-06-23 20:49:28
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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk