Topic: Constexpr Lambda Emulation in C++14


Author: Giovanni Piero Deretta <gpderetta@gmail.com>
Date: Wed, 24 Jun 2015 03:13:34 -0700 (PDT)
Raw View
------=_Part_1520_20722582.1435140814831
Content-Type: multipart/alternative;
 boundary="----=_Part_1521_392084860.1435140814831"

------=_Part_1521_392084860.1435140814831
Content-Type: text/plain; charset=UTF-8

[Reposting to larger audience and slightly rewording a message I sent to
boost-devel yesterday]

Hi All,

In the last few months constexpr lambdas and their absence in the
standard came up multiple times in this group.

I just wanted to share a C++14 emulation of the feature. The emulation
uses both the already known conditional operator trick (invented, I think,
by Paul Fultz II) to allow
constexpr 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
correct and I probably got some cases wrong. Lambda captures and
multiple statements 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

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_1521_392084860.1435140814831
Content-Type: text/html; charset=UTF-8

<div dir="ltr">[Reposting to larger audience and slightly rewording a message I sent to boost-devel yesterday]<br><br>Hi All,
<br>
<br>In the last few months constexpr lambdas and their absence in the
<br>standard came up multiple times in this group.<br>
<br>I just wanted to share a C++14 emulation of the feature. The emulation
<br>uses both the already known conditional operator trick (invented, I think, by Paul Fultz II) to allow
<br>constexpr function objects to be initialized from lambda expressions,
<br>plus what I think is a novel trick to allow the actual function object
<br>operator() to be constexpr.
<br>
<br>Implementation + example is here:
<br>
<br>&nbsp; &nbsp;<a href="https://github.com/gpderetta/libtask/blob/master/lambda.cpp" target="_blank" rel="nofollow">https://github.com/gpderetta/<wbr>libtask/blob/master/lambda.cpp</a>
<br>
<br>Sorry about the macro obfuscation, I was experimenting with a 'cute'
<br>lambda interface, but the underlying idea is very simple and pretty
<br>obvious in retrospect. I haven't tested that forwarding is always
<br>correct and I probably got some cases wrong. Lambda captures and
<br>multiple statements are doable, but are left as an exercise for the
<br>reader :)
<br>
<br>I came up with the trick when trying to implement an emulation of
<br>N3617 (Lifting overload sets into function objects). Turns out that
<br>implementing the 'quote' syntax is almost trivial in C++14 with
<br>generic lambdas, but making it constexpr reliably &nbsp;has so far eluded
<br>me as all implementations I have tried ICE both gcc and clang. Still
<br>the constexpr lambda emulation itself seems to work.
<br>
<br>HTH,
<br>
<br>-- gpd
</div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-proposals+unsubscribe@isocpp.org">std-proposals+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href="mailto:std-proposals@isocpp.org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href="http://groups.google.com/a/isocpp.org/group/std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/</a>.<br />

------=_Part_1521_392084860.1435140814831--
------=_Part_1520_20722582.1435140814831--

.