220 28214 <CAJnLdOaRwRm=tma=2trcYSY031SXvfkcXhQFBG2mS+0X8eqrpw@mail.gmail.com> article
Path: news.gmane.org!.POSTED!not-for-mail
From: "'Edward Catmur' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Re: capturing monads in lambda
Date: Sun, 11 Sep 2016 00:23:49 +0100
Lines: 147
Approved: news@gmane.org
Message-ID: <CAJnLdOaRwRm=tma=2trcYSY031SXvfkcXhQFBG2mS+0X8eqrpw@mail.gmail.com>
References: <5dba8d2b-6689-4781-a95a-c94896d98e55@isocpp.org>
 <2110a7c6-2b24-4d10-8bb3-0c022ae89849@isocpp.org> <973a28d5-552f-418a-9495-5a463ea61df4@isocpp.org>
 <6bd8c8b6-e293-430e-978a-08372b68aba9@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=001a1149c7a226b705053c2f8e0d
X-Trace: blaine.gmane.org 1473549837 23598 195.159.176.226 (10 Sep 2016 23:23:57 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Sat, 10 Sep 2016 23:23:57 +0000 (UTC)
Cc: szollosi.lorand@gmail.com
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDZLZTXF7UJBBBNM2K7AKGQEZDAKIFQ@isocpp.org Sun Sep 11 01:23:52 2016
Return-path: <std-proposals+bncBDZLZTXF7UJBBBNM2K7AKGQEZDAKIFQ@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pa0-f69.google.com ([209.85.220.69])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDZLZTXF7UJBBBNM2K7AKGQEZDAKIFQ@isocpp.org>)
	id 1bircj-0005Lr-FC
	for gclcip-std-proposals@m.gmane.org; Sun, 11 Sep 2016 01:23:49 +0200
Original-Received: by mail-pa0-f69.google.com with SMTP id le4sf15800883pab.3
        for <gclcip-std-proposals@m.gmane.org>; Sat, 10 Sep 2016 16:23:51 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=mime-version:in-reply-to:references:from:date:message-id:subject:to
         :cc:x-original-sender:x-original-authentication-results:reply-to
         :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post
         :list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=HcJ5fYxjQlphoyle4G4EKN9BUafOv/lL6E1isObPSqE=;
        b=Qn09pwvUON2wRKjlFD1n53nqelwYvsIKFXzX+eknf8TtGIatnK5mYrSM7ZQoEAdqGr
         69gr08yPHpt0CY0Pwkjk5/qVo1t8JIi022yzvjv/EygQkolEEMG6CmZdr9QueXN9nHPg
         QwUDnX3icql7j7enFhNmE0mOWiPEqdd60zihrZv2nBbO6qKQEeFL0UMEjZUfhEBDrD6W
         oWG+k9pM3MX3uTkk+rxr7/tfLitXvgRi5vmJUGpk22fPvwIycz/zn/kM6djjZWkdOSOW
         6nMFeiUC95MGImY4uyupfGhdb1zmoWuXvEcKCoW/qohZ6NGRE0TYWPJwNnzg6nobMSm0
         Wqeg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:in-reply-to:references:from:date
         :message-id:subject:to:cc:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=HcJ5fYxjQlphoyle4G4EKN9BUafOv/lL6E1isObPSqE=;
        b=E4HolWzivUgQONIGacb0ifdcu7xCP0uBPEfY8DzPvU4OOI1RltHSbLZpIBpRqAz77i
         UKU1zb0D+RNd+tlvDAUD2oOkJdQ92wZYslarC/XquSikBJiNG7SSHiEGDIEBkz34UjEg
         GbyYW4iPfAwDXklmHt68m289xdIm23JRiT4sKqwyXVFJSygxnTmdAXDAN6m0557T7BAm
         Ov0cpp6WTX7v+Q6oGVTOsHJ4OqXX1wwgIAt4QYlQNpcrn0l9JzfRy29RRy8YLf/JZ4c2
         r2GRML8+NwjLyB+Rd+OoVO9bHLQWgyGi44+eU2GbNIkeLIO7L8pn6r/41j/U07aXRKNK
         //gg==
X-Gm-Message-State: AE9vXwP6z/kW1moRKS8UPxsbgumv8OgnL4oA+S8CvlaujHANtJTtB/fFzYE8U2f1HqVMUw==
X-Received: by 10.66.148.129 with SMTP id ts1mr8679947pab.13.1473549830513;
        Sat, 10 Sep 2016 16:23:50 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.157.24.8 with SMTP id b8ls8998736ote.22.gmail; Sat, 10 Sep
 2016 16:23:49 -0700 (PDT)
X-Received: by 10.55.31.76 with SMTP id f73mr11311826qkf.260.1473549829771;
        Sat, 10 Sep 2016 16:23:49 -0700 (PDT)
Original-Received: from mail-qt0-x22f.google.com (mail-qt0-x22f.google.com. [2607:f8b0:400d:c0d::22f])
        by mx.google.com with ESMTPS id o46si5944763qta.89.2016.09.10.16.23.49
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Sat, 10 Sep 2016 16:23:49 -0700 (PDT)
Received-SPF: pass (google.com: domain of ecatmur@googlemail.com designates 2607:f8b0:400d:c0d::22f as permitted sender) client-ip=2607:f8b0:400d:c0d::22f;
Original-Received: by mail-qt0-x22f.google.com with SMTP id 11so57622537qtc.0
        for <std-proposals@isocpp.org>; Sat, 10 Sep 2016 16:23:49 -0700 (PDT)
X-Received: by 10.200.43.170 with SMTP id m39mr12267750qtm.73.1473549829583;
 Sat, 10 Sep 2016 16:23:49 -0700 (PDT)
Original-Received: by 10.237.32.135 with HTTP; Sat, 10 Sep 2016 16:23:49 -0700 (PDT)
Original-Received: by 10.237.32.135 with HTTP; Sat, 10 Sep 2016 16:23:49 -0700 (PDT)
In-Reply-To: <6bd8c8b6-e293-430e-978a-08372b68aba9@isocpp.org>
X-Original-Sender: ecatmur@googlemail.com
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@googlemail.com;       spf=pass (google.com: domain of
 ecatmur@googlemail.com designates 2607:f8b0:400d:c0d::22f as permitted
 sender) smtp.mailfrom=ecatmur@googlemail.com;       dmarc=pass (p=QUARANTINE
 dis=NONE) header.from=googlemail.com
X-Original-From: Edward Catmur <ecatmur@googlemail.com>
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <https://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <https://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <https://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:28214
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/28214>

--001a1149c7a226b705053c2f8e0d
Content-Type: text/plain; charset=UTF-8

On 10 Sep 2016 04:46, "Nicol Bolas" <jmckesson@gmail.com> wrote:
> See, it sounds like you're trying to say that returning from within the
lambda will cause it to return from... well, that right there is the
question. I think you want it to return from the function that called it.
But how exactly is that supposed to happen?

Stack unwinding.

> After all, the function that called it is not necessarily the function
that created it. What happens if you do this:
>
> ErrorCode save()
> {
>     std::function<void(ErrorCode)> return_if = [&return](auto retval){
>
>         if (retval) { return retval; }
>     };
>
>     return_if(openFile());
>     return_if(saveData());
>     return_if(closeFile());
> }
>
> I have every reason to expect this to achieve the same effect. Yet I
cannot imagine how that would be possible. After all, I don't call it. I
call `std::function::operator()`, which (eventually) calls the lambda. So
the lambda will just provoke the return of some construct within the
`std::function::operator()`.

Stack unwinding isn't just for exceptions. The machinery is there for non
local jumps, such as thread cancellation, setjmp and SEH.

> And you can't say that it will always return to the caller of the
lambda's creator, because I don't have to still be on the stack. I can
return that lambda to someone else, wrapped in a `std::function`. How does
that work?

UB, just as with capturing local variables by reference. Likewise if any of
the intervening frames are not unwinding aware.

> A lambda in C++ is not some magical construct, imbued with fantastical
powers. In C++, a lambda is an object. Not only that, it is an instance of
a class type. You can rewrite any lambda as a local struct with a
constructor and an operator() overload (except for generic lambdas, but I'd
like to see that fixed). As it currently stands, a lambda cannot do
anything that a user-created class can't also do.

There's another feature you're forgetting. Lambda captures can copy arrays
by value, which user code can't do without some heavy library machinery.

If we're talking user code equivalents, setjmp would do if it's
unwind-enabled, as would a custom exception in the absence of catch (...)
blocks, or even invoking the platform unwind machinery directly.

> <snip>

Sure, I get that you like that lambdas are mostly syntactic sugar. That was
probably necessary at the beginning, but it doesn't mean they have to stay
that way.

-- 
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAJnLdOaRwRm%3Dtma%3D2trcYSY031SXvfkcXhQFBG2mS%2B0X8eqrpw%40mail.gmail.com.

--001a1149c7a226b705053c2f8e0d
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<p dir=3D"ltr"></p>
<p dir=3D"ltr">On 10 Sep 2016 04:46, &quot;Nicol Bolas&quot; &lt;<a href=3D=
"mailto:jmckesson@gmail.com">jmckesson@gmail.com</a>&gt; wrote:<br>
&gt; See, it sounds like you&#39;re trying to say that returning from withi=
n the lambda will cause it to return from... well, that right there is the =
question. I think you want it to return from the function that called it. B=
ut how exactly is that supposed to happen?</p>
<p dir=3D"ltr">Stack unwinding. </p>
<p dir=3D"ltr">&gt; After all, the function that called it is not necessari=
ly the function that created it. What happens if you do this:<br>
&gt;<br>
&gt; ErrorCode save()<br>
&gt; {<br>
&gt; =C2=A0 =C2=A0 std::function&lt;void(ErrorCode)&gt; return_if =3D [&amp=
;return](auto retval){<br>
&gt;<br>
&gt; =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (retval) { return retval; }<br>
&gt; =C2=A0 =C2=A0 };<br>
&gt;<br>
&gt; =C2=A0 =C2=A0 return_if(openFile());<br>
&gt; =C2=A0 =C2=A0 return_if(saveData());<br>
&gt; =C2=A0 =C2=A0 return_if(closeFile());<br>
&gt; }<br>
&gt;<br>
&gt; I have every reason to expect this to achieve the same effect. Yet I c=
annot imagine how that would be possible. After all, I don&#39;t call it. I=
 call `std::function::operator()`, which (eventually) calls the lambda. So =
the lambda will just provoke the return of some construct within the `std::=
function::operator()`.</p>
<p dir=3D"ltr">Stack unwinding isn&#39;t just for exceptions. The machinery=
 is there for non local jumps, such as thread cancellation, setjmp and SEH.=
 </p>
<p dir=3D"ltr">&gt; And you can&#39;t say that it will always return to the=
 caller of the lambda&#39;s creator, because I don&#39;t have to still be o=
n the stack. I can return that lambda to someone else, wrapped in a `std::f=
unction`. How does that work?</p>
<p dir=3D"ltr">UB, just as with capturing local variables by reference. Lik=
ewise if any of the intervening frames are not unwinding aware. </p>
<p dir=3D"ltr">&gt; A lambda in C++ is not some magical construct, imbued w=
ith fantastical powers. In C++, a lambda is an object. Not only that, it is=
 an instance of a class type. You can rewrite any lambda as a local struct =
with a constructor and an operator() overload (except for generic lambdas, =
but I&#39;d like to see that fixed). As it currently stands, a lambda canno=
t do anything that a user-created class can&#39;t also do.</p>
<p dir=3D"ltr">There&#39;s another feature you&#39;re forgetting. Lambda ca=
ptures can copy arrays by value, which user code can&#39;t do without some =
heavy library machinery. </p>
<p dir=3D"ltr">If we&#39;re talking user code equivalents, setjmp would do =
if it&#39;s unwind-enabled, as would a custom exception in the absence of c=
atch (...) blocks, or even invoking the platform unwind machinery directly.=
 </p>
<p dir=3D"ltr">&gt; &lt;snip&gt;</p>
<p dir=3D"ltr">Sure, I get that you like that lambdas are mostly syntactic =
sugar. That was probably necessary at the beginning, but it doesn&#39;t mea=
n they have to stay that way. </p>

<p></p>

-- <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 e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CAJnLdOaRwRm%3Dtma%3D2trcYSY031SXvfkc=
XhQFBG2mS%2B0X8eqrpw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfoote=
r">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAJnLdOaRwR=
m%3Dtma%3D2trcYSY031SXvfkcXhQFBG2mS%2B0X8eqrpw%40mail.gmail.com</a>.<br />

--001a1149c7a226b705053c2f8e0d--

.
