220 27407 <CAGa0LWtm1z9Ysynmfa8L2G=7_aFn7YcSbpBHSYPDkuuR1qh4OA@mail.gmail.com> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Carl Cook <carl.cook@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Proposal - non allocating std::function
Date: Sun, 31 Jul 2016 16:25:44 +0200
Lines: 191
Approved: news@gmane.org
Message-ID: <CAGa0LWtm1z9Ysynmfa8L2G=7_aFn7YcSbpBHSYPDkuuR1qh4OA@mail.gmail.com>
References: <e00db911-40f5-4c11-a4b8-32bba77aa0a5@isocpp.org>
 <CANh-dXnpUNf2O5NTj213K1Jfmhge1=eit6+SistzYWZj+5CkNg@mail.gmail.com>
 <ff1087cf-5777-47e9-8caa-d28136aae130@isocpp.org> <CAOfiQqn31H_Mfkxx3yty1=wqu50JnpcSv25uLP3DNDTb5E8WYQ@mail.gmail.com>
 <cd6e299d-7cec-4d8e-8a55-f25fd88d1b91@isocpp.org> <CAOfiQqmAmuesO=jq+Kw3XYmS0B_j32LZ25oyw+XtykMtOYGHbg@mail.gmail.com>
 <c3bc751f-5586-44d5-ba6c-4c3d6708e4ad@isocpp.org> <ee95d8af-e8e9-47bc-9d61-d95551aac4f3@isocpp.org>
 <CAGa0LWv74_kmDz5Ewrp9bDOmNddTZOHcu17J3mN16ibv9aaVNQ@mail.gmail.com> <CAGa0LWue8b2uX4thh3450GS7eo+jg=xFP_JxVsXNB3f_Dtpt3Q@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=001a11443c786687530538ef4216
X-Trace: blaine.gmane.org 1469975177 27586 80.91.229.8 (31 Jul 2016 14:26:17 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Sun, 31 Jul 2016 14:26:17 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBD27VEPGSEGBB3EU7C6AKGQEH3ABMHI@isocpp.org Sun Jul 31 16:25:56 2016
Return-path: <std-proposals+bncBD27VEPGSEGBB3EU7C6AKGQEH3ABMHI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-lf0-f72.google.com ([209.85.215.72])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBD27VEPGSEGBB3EU7C6AKGQEH3ABMHI@isocpp.org>)
	id 1bTrgf-00073Z-NI
	for gclcip-std-proposals@m.gmane.org; Sun, 31 Jul 2016 16:25:53 +0200
Original-Received: by mail-lf0-f72.google.com with SMTP id 33sf60935180lfw.1
        for <gclcip-std-proposals@m.gmane.org>; Sun, 31 Jul 2016 07:25:53 -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
         :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=fmZTHQgKM/pclqScCNQBRVF9ZHyI4yMEQl5l+41Phjo=;
        b=LawafLge3BWqw9Pl5NmX0t3XZArZ6x9l02zPBOYV7MWv0b3C0EcKSfJJJhXVXzyYai
         d3uhsNoNc1p9KqBMYpvg3av5URt1dAis70ZX9OQ7hbJdjCTkFZtDiZP6MVq8/JSDoDtJ
         n1QOhVHNbpX12oz4oQsCYL+1/qa9TBZJjGz90mJ+lGeCkTSut82Kq2scZ+UQcfP1EkYJ
         DL/t9b9BooGtkgzLYxHC3MuG4vkZJ4ehzrojKBXCZk8GDrsImK9Mk+ydbG6AIog7Vn0C
         yoMk2r4HVcIjgVYyJgEM8kq2dEBeplalbDn1cd7rS9UOCxgHbpuikMRTSJBAlJfdJt8m
         g00Q==
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: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=fmZTHQgKM/pclqScCNQBRVF9ZHyI4yMEQl5l+41Phjo=;
        b=N5KaLSKSqHeqi5BsJa1XndabPPUYDzRVeE0GRZKzPQpcwJ6ZZclmTOLyt3RG4fk0KM
         K32nzrnPvAJLImBMy4tX6PU7WHyuphs26HqjDeu1eljYX+LfAR4hVoojyzrrqHoRUkJu
         K6Ib17N3ukrmMI5sGnSFDlJDfDorpLYEP4KeGzHkO4xawP4LaVJnNkEzTYVLELO+FYCC
         ef9P/gR6xIbJ98Lpd1CeB6ieXwR72mg2TUhXSVNejO6/KXyx2qpqDxBhtI1Tli3bRB/7
         oMkqywvNgVJ/DmHWXAxcKyTmDnYq0DJZGOTHF1orFd7Se/3W7xiQHEo3rj1TmlTh4Q1T
         /RiQ==
X-Gm-Message-State: AEkoouuvMItYHt+rgqOU7VJkezOehSUaqqz9Xyi/h7/vBmeRe7xQ6NRf6DFKsjd5vQJRWw==
X-Received: by 10.46.1.25 with SMTP id 25mr7262554ljb.10.1469975153378;
        Sun, 31 Jul 2016 07:25:53 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.28.70.130 with SMTP id t124ls1342895wma.16.gmail; Sun, 31 Jul
 2016 07:25:47 -0700 (PDT)
X-Received: by 10.195.12.111 with SMTP id ep15mr45636985wjd.124.1469975146311;
        Sun, 31 Jul 2016 07:25:46 -0700 (PDT)
Original-Received: from mail-wm0-x22e.google.com (mail-wm0-x22e.google.com. [2a00:1450:400c:c09::22e])
        by mx.google.com with ESMTPS id z13si27426315wjr.64.2016.07.31.07.25.46
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Sun, 31 Jul 2016 07:25:46 -0700 (PDT)
Received-SPF: pass (google.com: domain of carl.cook@gmail.com designates 2a00:1450:400c:c09::22e as permitted sender) client-ip=2a00:1450:400c:c09::22e;
Original-Received: by mail-wm0-x22e.google.com with SMTP id o80so210741660wme.1
        for <std-proposals@isocpp.org>; Sun, 31 Jul 2016 07:25:46 -0700 (PDT)
X-Received: by 10.28.51.21 with SMTP id z21mr53974534wmz.24.1469975145906;
 Sun, 31 Jul 2016 07:25:45 -0700 (PDT)
Original-Received: by 10.194.178.99 with HTTP; Sun, 31 Jul 2016 07:25:44 -0700 (PDT)
Original-Received: by 10.194.178.99 with HTTP; Sun, 31 Jul 2016 07:25:44 -0700 (PDT)
In-Reply-To: <CAGa0LWue8b2uX4thh3450GS7eo+jg=xFP_JxVsXNB3f_Dtpt3Q@mail.gmail.com>
X-Original-Sender: carl.cook@gmail.com
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@gmail.com;       spf=pass (google.com: domain of
 carl.cook@gmail.com designates 2a00:1450:400c:c09::22e as permitted sender)
 smtp.mailfrom=carl.cook@gmail.com;       dmarc=pass (p=NONE dis=NONE) header.from=gmail.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:27407
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/27407>

--001a11443c786687530538ef4216
Content-Type: text/plain; charset=UTF-8

Understood, thanks. Hence llvm::function_ref really is what it says. I.e.
don't use it beyond references to functions.

On 31 Jul 2016 16:15, "Casey Carter" <cartec69@gmail.com> wrote:

On Sunday, July 31, 2016 at 6:29:30 AM UTC-7, Carl Cook wrote:
>
> In the example below, I am not passing a function_ref as the return value,
> I am returning the result of the invocation of that function_ref, which I'd
> assumed would be safe (i.e. return an integer).
>
> However, it is the invocation that has failed, because the compiler has
> decided that the lambda can already be cleaned up (from what I can tell,
> looking at the disassembly).
>
> So, granted that the function_ref certainly doesn't copy callable objects,
> but I don't think it's useful for any lambda, unless that lambda is just
> used to invoke a function (as in the llvm example usage).
>
> On Friday, 15 July 2016 20:27:56 UTC+2, Richard Smith wrote:
>>
>> On Fri, Jul 15, 2016 at 2:12 AM, Carl Cook <carl...@gmail.com> wrote:
>>
>>> On Friday, 15 July 2016 02:35:42 UTC+2, Richard Smith wrote:
>>>>
>>>> llvm::function_ref doesn't capture anything, and is not supposed to --
>>>> it's a *non-owning* handle to a callable that outlives the handle.
>>>> (llvm::function_ref is to std::function as std::string_view is to
>>>> std::string.) This is a feature and a design goal of llvm::function_ref,
>>>> but I think it means it's addressing a fundamentally different problem than
>>>> the one you're tackling here (which seems to be essentially, "let me
>>>> control the size and alignment in std::function's small function
>>>> optimization"
>>>>
>>>
>>> Understood. What caught me out is that compilers can aggressively limit
>>> the scope of lambdas. Hence I believe the following is not legal usage:
>>>
>>> int non_const_var = 0xf;
>>> function_ref<int()> f_ref = [&]() { return non_const_var;};
>>> return f_ref();
>>>
>>
>> Right, it's exactly as broken as this:
>>
>> string g() {
>>   string_view s_v = string("blah blah");
>>   return s_v;
>> }
>>
>> In both cases, you've bound an object with reference semantics to a
>> (non-lifetime-extended) temporary object.
>>
>
[&]() { return non_const_var;}; is an expression whose value is a temporary
object. That temporary object is destroyed at the end of the full
expression
function_ref<int()> f_ref = [&]() { return non_const_var;};
Using the reference stored in f_ref to access that object after the end of
its lifetime results in undefined behavior.

-- 
You received this message because you are subscribed to a topic in the
Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this topic, visit
https://groups.google.com/a/isocpp.org/d/topic/std-proposals/vven2Om7Ha8/unsubscribe
..
To unsubscribe from this group and all its topics, 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/ee95d8af-e8e9-47bc-9d61-d95551aac4f3%40isocpp.org
<https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/ee95d8af-e8e9-47bc-9d61-d95551aac4f3%40isocpp.org?utm_medium=email&utm_source=footer>
..

-- 
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/CAGa0LWtm1z9Ysynmfa8L2G%3D7_aFn7YcSbpBHSYPDkuuR1qh4OA%40mail.gmail.com.

--001a11443c786687530538ef4216
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<p dir=3D"ltr">Understood, thanks. Hence llvm::function_ref really is what =
it says. I.e. don&#39;t use it beyond references to functions.</p>
<div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On 31 Jul 2016 16=
:15, &quot;Casey Carter&quot; &lt;<a href=3D"mailto:cartec69@gmail.com">car=
tec69@gmail.com</a>&gt; wrote:<br type=3D"attribution"><blockquote class=3D=
"quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:=
1ex"><div dir=3D"ltr"><div class=3D"quoted-text">On Sunday, July 31, 2016 a=
t 6:29:30 AM UTC-7, Carl Cook wrote:<blockquote class=3D"gmail_quote" style=
=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"=
><div dir=3D"ltr">In the example below, I am not passing a function_ref as =
the return value, I am returning the result of the invocation of that funct=
ion_ref, which I&#39;d assumed would be safe (i.e. return an integer).<div>=
<br></div><div>However, it is the invocation that has failed, because the c=
ompiler has decided that the lambda can already be cleaned up (from what I =
can tell, looking at the disassembly).</div><div><br></div><div>So, granted=
 that the function_ref certainly doesn&#39;t copy callable objects, but I d=
on&#39;t think it&#39;s useful for any lambda, unless that lambda is just u=
sed to invoke a function (as in the llvm example usage).<br><br>On Friday, =
15 July 2016 20:27:56 UTC+2, Richard Smith  wrote:<blockquote class=3D"gmai=
l_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;pad=
ding-left:1ex"><div dir=3D"ltr"><div><div class=3D"gmail_quote">On Fri, Jul=
 15, 2016 at 2:12 AM, Carl Cook <span dir=3D"ltr">&lt;<a rel=3D"nofollow">c=
arl...@gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote"=
 style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><d=
iv dir=3D"ltr"><span>On Friday, 15 July 2016 02:35:42 UTC+2, Richard Smith =
 wrote:<blockquote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8e=
x;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div class=
=3D"gmail_quote"><div>llvm::function_ref doesn&#39;t capture anything, and =
is not supposed to -- it&#39;s a *non-owning* handle to a callable that out=
lives the handle. (llvm::function_ref is to std::function as std::string_vi=
ew is to std::string.) This is a feature and a design goal of llvm::functio=
n_ref, but I think it means it&#39;s addressing a fundamentally different p=
roblem than the one you&#39;re tackling here (which seems to be essentially=
, &quot;let me control the size and alignment in std::function&#39;s small =
function optimization&quot;</div></div></div></blockquote><div><br></div></=
span><div>Understood. What caught me out is that compilers can aggressively=
 limit the scope of lambdas. Hence I believe the following is not legal usa=
ge:</div><div><br></div><div><div style=3D"border:1px solid rgb(187,187,187=
);word-wrap:break-word;background-color:rgb(250,250,250)"><code><div><div>i=
nt non_const_var =3D 0xf;</div><div>function_ref&lt;int()&gt; f_ref =3D [&a=
mp;]() { return non_const_var;};</div><div>return f_ref();</div></div></cod=
e></div></div></div></blockquote><div><br></div><div>Right, it&#39;s exactl=
y as broken as this:</div><div><br></div><div>string g() {</div><div>=C2=A0=
 string_view s_v =3D string(&quot;blah blah&quot;);</div><div>=C2=A0 return=
 s_v;</div><div>}</div><div><br></div><div>In both cases, you&#39;ve bound =
an object with reference semantics to a (non-lifetime-extended) temporary o=
bject.</div></div></div></div></blockquote></div></div></blockquote><div><b=
r></div></div><div><font face=3D"courier new, monospace">[&amp;]() { return=
 non_const_var;};</font> is an expression whose value is a temporary object=
.. That temporary object is destroyed at the end of the full expression <div=
 class=3D"quoted-text"><font face=3D"courier new, monospace"><div style=3D"=
border:1px solid rgb(187,187,187);word-wrap:break-word;background-color:rgb=
(250,250,250)"><code><div><span style=3D"color:#000">function_ref</span><sp=
an style=3D"color:#660">&lt;</span><span style=3D"color:#008">int</span><sp=
an style=3D"color:#660">()&gt;</span><span style=3D"color:#000"> f_ref </sp=
an><span style=3D"color:#660">=3D</span><span style=3D"color:#000"> </span>=
<span style=3D"color:#660">[&amp;]()</span><span style=3D"color:#000"> </sp=
an><span style=3D"color:#660">{</span><span style=3D"color:#000"> </span><s=
pan style=3D"color:#008">return</span><span style=3D"color:#000"> non_const=
_var</span><span style=3D"color:#660">;};</span></div></code></div></font><=
/div></div><div><font face=3D"arial, sans-serif">Using the reference stored=
 in f_ref to access that object after the end of its lifetime results in un=
defined behavior.</font></div></div><div class=3D"quoted-text">

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/vven2Om7Ha8/unsubscribe" target=3D"_blan=
k">https://groups.google.com/a/isocpp.org/d/topic/std-proposals/vven2Om7Ha8=
/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_blank">std-prop=
osals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br></div>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/ee95d8af-e8e9-47bc-9d61-d95551aac4f3%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/ee95d8af-e8e9-=
47bc-9d61-d95551aac4f3%40isocpp.org</a>.<br>
</blockquote></div><br></div>

<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/CAGa0LWtm1z9Ysynmfa8L2G%3D7_aFn7YcSbp=
BHSYPDkuuR1qh4OA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAGa0LWtm1z9Ysy=
nmfa8L2G%3D7_aFn7YcSbpBHSYPDkuuR1qh4OA%40mail.gmail.com</a>.<br />

--001a11443c786687530538ef4216--

.
