220 27453 <CAGa0LWsUjCP5j8ra_F1mZczRXjdwgkhhFdOLDYS7Jm982v4iyQ@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: Mon, 1 Aug 2016 18:26:35 +0200
Lines: 231
Approved: news@gmane.org
Message-ID: <CAGa0LWsUjCP5j8ra_F1mZczRXjdwgkhhFdOLDYS7Jm982v4iyQ@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> <CAOfiQq=K+GLh1GMBBzULYGNLUU4oeotAD0Lgn-S=wSv0hPWoog@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=001a1145b52c72ec8e0539051045
X-Trace: blaine.gmane.org 1470068803 30024 195.159.176.226 (1 Aug 2016 16:26:43 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Mon, 1 Aug 2016 16:26:43 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBD27VEPGSEGBBPXQ7W6AKGQEQGTCCDI@isocpp.org Mon Aug 01 18:26:38 2016
Return-path: <std-proposals+bncBD27VEPGSEGBBPXQ7W6AKGQEQGTCCDI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-wm0-f69.google.com ([74.125.82.69])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBD27VEPGSEGBBPXQ7W6AKGQEQGTCCDI@isocpp.org>)
	id 1bUG34-0007Yu-EX
	for gclcip-std-proposals@m.gmane.org; Mon, 01 Aug 2016 18:26:38 +0200
Original-Received: by mail-wm0-f69.google.com with SMTP id p129sf85564780wmp.3
        for <gclcip-std-proposals@m.gmane.org>; Mon, 01 Aug 2016 09:26:38 -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=bEsZb0cBdURzc1xMR0P/Kw2s0+gLCsYDIf90C8cfErg=;
        b=FowvBK52by+b3lsOdudtjzpu5stgBnu+9GfTvG3iWc50Yh5LxF6vyvV8tR3+XPVtBT
         1uybU+R0Pzf83sjEp8MZBYgqS45jP0JiWsrzSpsppIQtmcptj6nWGsOCCKIZViIvd8oa
         i7YRXJfUA7dPjF9snKadX5PNkbR65Oy7aRhfJSNssk7pGIUinRaYmfLxJ3McIsclp1K+
         OVqbs/lR0wFjfFpbJ4bqEOYOO6NT8FT+JCLTI25O1t9sSyCcvRr38gCzMTmksgxu3zWx
         vmJkZy7P0EnN2ZYnYwvRJHw2fb+ydMgtaXEY34Y6wmyFOQN4/Zt7hLLdb7BMuWrIQhQ1
         lejw==
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=bEsZb0cBdURzc1xMR0P/Kw2s0+gLCsYDIf90C8cfErg=;
        b=mLBZqGOMZm4lR3V69vnr8VQJr5rnbuOW4O1HKcZx8olXacmB5YY/bhlvFnF4orXfHV
         4/TwuTOlLFPCebBWpeD5N75pxOvtIBJVBHW9pgnVI/uJHvGj43mCmgNDPMqFbyK1Ms3S
         /uGg0mt5ElpwLHpy4VnAMyN09/NBM1M2JF+haLfHHT1Sn5u9hpGvZAAmK6fI6IuvXepg
         VMRcrCA2gQj07TFfcjlusrZKQi6Tr1QvUKurhLp4JN06s0KbEvBDBuBTlXdKpqbWA92S
         aiP5ViGq3VhVFe7AtO5f7AZO131xDxP6S/TpoQFvezLEI/R2hsljOxcuMVPtFj2Hx6Gt
         oA4A==
X-Gm-Message-State: AEkooutsgDCTqxu3OrZP0mQr6SvAVeG8+Zdvcj2cmlEGilaiD2YB49Zb5nN30Fob5Drk2w==
X-Received: by 10.28.203.3 with SMTP id b3mr7969016wmg.3.1470068798566;
        Mon, 01 Aug 2016 09:26:38 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.28.196.15 with SMTP id u15ls568324wmf.10.canary; Mon, 01 Aug
 2016 09:26:37 -0700 (PDT)
X-Received: by 10.194.28.5 with SMTP id x5mr50533209wjg.11.1470068797542;
        Mon, 01 Aug 2016 09:26:37 -0700 (PDT)
Original-Received: from mail-wm0-x230.google.com (mail-wm0-x230.google.com. [2a00:1450:400c:c09::230])
        by mx.google.com with ESMTPS id mn7si32227113wjc.177.2016.08.01.09.26.37
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Mon, 01 Aug 2016 09:26:37 -0700 (PDT)
Received-SPF: pass (google.com: domain of carl.cook@gmail.com designates 2a00:1450:400c:c09::230 as permitted sender) client-ip=2a00:1450:400c:c09::230;
Original-Received: by mail-wm0-x230.google.com with SMTP id o80so252389129wme.1
        for <std-proposals@isocpp.org>; Mon, 01 Aug 2016 09:26:37 -0700 (PDT)
X-Received: by 10.28.20.139 with SMTP id 133mr15745904wmu.78.1470068797137;
 Mon, 01 Aug 2016 09:26:37 -0700 (PDT)
Original-Received: by 10.194.178.99 with HTTP; Mon, 1 Aug 2016 09:26:35 -0700 (PDT)
Original-Received: by 10.194.178.99 with HTTP; Mon, 1 Aug 2016 09:26:35 -0700 (PDT)
In-Reply-To: <CAOfiQq=K+GLh1GMBBzULYGNLUU4oeotAD0Lgn-S=wSv0hPWoog@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::230 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:27453
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/27453>

--001a1145b52c72ec8e0539051045
Content-Type: text/plain; charset=UTF-8

I didn't spot that you are indeed returning a string. My mistake. Thanks,
yes now I see that your example holds perfectly.

On 1 Aug 2016 18:23, "Richard Smith" <richard@metafoo.co.uk> wrote:

> On Sun, Jul 31, 2016 at 6:29 AM, Carl Cook <carl.cook@gmail.com> 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).
>>
>
> Actually, the above is exactly analogous to what happens in the
> string_view case. Note that I'm returning a string, not a string_view.
>
> In both cases, the problem happens in the construction of the
> string_view/function_ref object: we're constructing an object that
> represents a handle to another object, and the other object is a temporary
> object that goes away at the end of the full-expression. And then on the
> second line, we do something that tries to access the now-destroyed object
> through the handle (in the string_view case, we construct a string from the
> string_view, and in the function_ref case, we attempt to call the destroyed
> closure object).
>
> This is just how handle-like objects behave when bound to temporaries, and
> not specific to string_view or function_ref.
>
>
>> 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.
>>>
>> --
>> 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/c3bc751f-5586-44d5-ba6c-4c3d6708e4ad%40isocpp.org
>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/c3bc751f-5586-44d5-ba6c-4c3d6708e4ad%40isocpp.org?utm_medium=email&utm_source=footer>
>> .
>>
>
> --
> 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/CAOfiQq%3DK%2BGLh1GMBBzULYGNLUU4oeotAD0Lgn-S%3DwSv0hPWoog%40mail.gmail.com
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAOfiQq%3DK%2BGLh1GMBBzULYGNLUU4oeotAD0Lgn-S%3DwSv0hPWoog%40mail.gmail.com?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/CAGa0LWsUjCP5j8ra_F1mZczRXjdwgkhhFdOLDYS7Jm982v4iyQ%40mail.gmail.com.

--001a1145b52c72ec8e0539051045
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<p dir=3D"ltr">I didn&#39;t spot that you are indeed returning a string. My=
 mistake. Thanks, yes now I see that your example holds perfectly.</p>
<div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On 1 Aug 2016 18:=
23, &quot;Richard Smith&quot; &lt;<a href=3D"mailto:richard@metafoo.co.uk">=
richard@metafoo.co.uk</a>&gt; wrote:<br type=3D"attribution"><blockquote cl=
ass=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;p=
adding-left:1ex"><div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"=
gmail_quote">On Sun, Jul 31, 2016 at 6:29 AM, Carl Cook <span dir=3D"ltr">&=
lt;<a href=3D"mailto:carl.cook@gmail.com" target=3D"_blank">carl.cook@gmail=
..com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"ma=
rgin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"lt=
r">In the example below, I am not passing a function_ref as the return valu=
e, I am returning the result of the invocation of that function_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 compiler has dec=
ided that the lambda can already be cleaned up (from what I can tell, looki=
ng at the disassembly).</div><div><br></div><div>So, granted that the funct=
ion_ref certainly doesn&#39;t copy callable objects, but I don&#39;t think =
it&#39;s useful for any lambda, unless that lambda is just used to invoke a=
 function (as in the llvm example usage).<br></div></div></blockquote><div>=
<br></div><div>Actually, the above is exactly analogous to what happens in =
the string_view case. Note that I&#39;m returning a string, not a string_vi=
ew.</div><div><br></div><div>In both cases, the problem happens in the cons=
truction of the string_view/function_ref object: we&#39;re constructing an =
object that represents a handle to another object, and the other object is =
a temporary object that goes away at the end of the full-expression. And th=
en on the second line, we do something that tries to access the now-destroy=
ed object through the handle (in the string_view case, we construct a strin=
g from the string_view, and in the function_ref case, we attempt to call th=
e destroyed closure object).</div><div><br></div><div>This is just how hand=
le-like objects behave when bound to temporaries, and not specific to strin=
g_view or function_ref.</div><div>=C2=A0</div><blockquote class=3D"gmail_qu=
ote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex=
"><div dir=3D"ltr"><div>On Friday, 15 July 2016 20:27:56 UTC+2, Richard Smi=
th  wrote:<span><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"><d=
iv><div class=3D"gmail_quote">On Fri, Jul 15, 2016 at 2:12 AM, Carl Cook <s=
pan dir=3D"ltr">&lt;<a rel=3D"nofollow">carl...@gmail.com</a>&gt;</span> wr=
ote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border=
-left:1px #ccc solid;padding-left:1ex"><div 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.8ex;border-left:1px #ccc solid;paddi=
ng-left:1ex"><div dir=3D"ltr"><div class=3D"gmail_quote"><div>llvm::functio=
n_ref doesn&#39;t capture anything, and is not supposed to -- it&#39;s a *n=
on-owning* handle to a callable that outlives the handle. (llvm::function_r=
ef is to std::function as std::string_view is to std::string.) This is a fe=
ature and a design goal of llvm::function_ref, but I think it means it&#39;=
s addressing a fundamentally different problem than the one you&#39;re tack=
ling 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 m=
e out is that compilers can aggressively limit the scope of lambdas. Hence =
I believe the following is not legal usage:</div><div><br></div><div><div s=
tyle=3D"border:1px solid rgb(187,187,187);word-wrap:break-word;background-c=
olor:rgb(250,250,250)"><code><div><div>int non_const_var =3D 0xf;</div><div=
>function_ref&lt;int()&gt; f_ref =3D [&amp;]() { return non_const_var;};</d=
iv><div>return f_ref();</div></div></code></div></div></div></blockquote><d=
iv><br></div><div>Right, it&#39;s exactly as broken as this:</div><div><br>=
</div><div>string g() {</div><div>=C2=A0 string_view s_v =3D string(&quot;b=
lah blah&quot;);</div><div>=C2=A0 return s_v;</div><div>}</div><div><br></d=
iv><div>In both cases, you&#39;ve bound an object with reference semantics =
to a (non-lifetime-extended) temporary object.</div></div></div></div>
</blockquote></span></div></div>

<p></p>

-- <br><span>
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" target=3D"_=
blank">std-proposals+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></span>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/c3bc751f-5586-44d5-ba6c-4c3d6708e4ad%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/c3bc751f-5586-=
44d5-ba6c-4c3d6708e4ad%40isocpp.org</a>.<br>
</blockquote></div><br></div></div>

<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>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CAOfiQq%3DK%2BGLh1GMBBzULYGNLUU4oeotA=
D0Lgn-S%3DwSv0hPWoog%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Df=
ooter" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std=
-proposals/CAOfiQq%3DK%2BGLh1GMBBzULYGNLUU4oeotAD0Lgn-S%3DwSv0hPWoog%40mail=
..gmail.com</a>.<br>
</blockquote></div></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/CAGa0LWsUjCP5j8ra_F1mZczRXjdwgkhhFdOL=
DYS7Jm982v4iyQ%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">htt=
ps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAGa0LWsUjCP5j8ra=
_F1mZczRXjdwgkhhFdOLDYS7Jm982v4iyQ%40mail.gmail.com</a>.<br />

--001a1145b52c72ec8e0539051045--

.
