220 37726 <e2229872-0c8a-4673-b452-602959fc938f@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Hamza Sood <hamza_sood@me.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Generic function-wrapper object
Date: Mon, 9 Apr 2018 08:36:14 -0700 (PDT)
Lines: 207
Approved: news@gmane.org
Message-ID: <e2229872-0c8a-4673-b452-602959fc938f@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_23773_924158111.1523288174475"
X-Trace: blaine.gmane.org 1523288051 5112 195.159.176.226 (9 Apr 2018 15:34:11 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Mon, 9 Apr 2018 15:34:11 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCP5NIM2SYKRB34QV3LAKGQEA5FPCCA@isocpp.org Mon Apr 09 17:34:07 2018
Return-path: <std-proposals+bncBCP5NIM2SYKRB34QV3LAKGQEA5FPCCA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-vk0-f71.google.com ([209.85.213.71])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCP5NIM2SYKRB34QV3LAKGQEA5FPCCA@isocpp.org>)
	id 1f5Yo2-0001CQ-W9
	for gclcip-std-proposals@m.gmane.org; Mon, 09 Apr 2018 17:34:07 +0200
Original-Received: by mail-vk0-f71.google.com with SMTP id p10sf2954728vki.23
        for <gclcip-std-proposals@m.gmane.org>; Mon, 09 Apr 2018 08:36:16 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:message-id:subject:mime-version:x-original-sender
         :reply-to:precedence:mailing-list:list-id:list-post:list-help
         :list-archive:list-subscribe:list-unsubscribe;
        bh=KKJgZfhdXJprXsD+Iuo7Y0l2yM6oUmafW1sgwSdjQsA=;
        b=M2aLL0dB+/wAEUGgNtSYbIsy/jxvEn85Ama1TDLJPeQErqoxn0R/ybg2u5caTrnVub
         oNmhwM98lstLimTLB5PnP6pjZPI1UA4k6ubtXPV/h/oXPVDOduGv4LoZrm4ndCrSGfsR
         lLM6TA/HNAIbd1KsHUnAdPdCqVl7/CcH8HDUGLnfKNUPOa5hYTaXePhwcFidaC86hhXq
         XLnco0zw90QtONhFpeu1dN9Vgj/almWvAu1i2IaDyAsavhBgaCCdXtNh8G2lxkMmGUxa
         jpbRVutFHPfjOzenIC1Q63pluBOT/0UPB0PvyDo45wn+Noxtmjd/ECxSqwoKC0QDJeDj
         ozKw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:date:from:to:message-id:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=KKJgZfhdXJprXsD+Iuo7Y0l2yM6oUmafW1sgwSdjQsA=;
        b=q5pHJpXoZSQRMEGiZCgEs9/nCDp1fVl7BT9vBZLC8YnIREu6vddOwkQCW8raEBySEn
         OfLxeWydrdpxQOLam9JuuNHdhs8w/6G6X+LiXRvFfe8fOwoABr5YqEkfmFZE9x/b581D
         htp7AZDQE9nY5B85AEdLuf4RKX9v0uQAEnA01YvZAbrXxuY/2wOs2nRngvQOA2KFKT81
         llqjwnXGlMEEj1bq26TqF4OT1FzMteYq/zyab8a4lg5ugKJiaG26upG2DFm9zKrXXaWO
         ruZLLAxh5x3/tV1RYQLFJCZX4+6Y3TfqITlIer/veA1ppJRHPVeKPXzilPcwHK1nC6m1
         z/Wg==
X-Gm-Message-State: ALQs6tDky55tR5e52u0rDSFYtu+0zkrRHXut0b0D2c6PuIcYVgtA0ljc
	ZNu/JRTdo+UEFFn4r17tJe3yrA==
X-Google-Smtp-Source: AIpwx4+wK8m1/QG88q11cWvCrVFGJvXcoqvK/9Nx0zZBanQOJ/C0wjbnT7vxWz0Q5g0rYgBL72Sa4A==
X-Received: by 10.176.112.34 with SMTP id u2mr16644968ual.9.1523288176376;
        Mon, 09 Apr 2018 08:36:16 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.31.48.81 with SMTP id w78ls2664340vkw.19.gmail; Mon, 09 Apr
 2018 08:36:15 -0700 (PDT)
X-Received: by 10.31.140.138 with SMTP id o132mr4644168vkd.8.1523288174808;
        Mon, 09 Apr 2018 08:36:14 -0700 (PDT)
X-Original-Sender: hamza_sood@me.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Spam-Checked-In-Group: 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:37726
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/37726>

------=_Part_23773_924158111.1523288174475
Content-Type: multipart/alternative; 
	boundary="----=_Part_23774_1996392239.1523288174476"

------=_Part_23774_1996392239.1523288174476
Content-Type: text/plain; charset="UTF-8"

I propose the inclusion of a function object (somewhat similar to the 
deprecated ptr_fun) that looks something like:

template<auto f>
requires is_pointer_v<decltype(f)> &&
         is_function_v<remove_pointer_t<decltype(f)>>
struct func_ptr {
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) noexcept(noexcept(f(forward<Args
>(args)...))) {
    return f(forward<Args>(args)...);
  }
};

The main difference with ptr_fun being that the function pointer is part of 
the type.



*Motivation*

The main motivating example is a unique_ptr that needs to call a custom 
deleter function. Currently (assuming you don't want to write some 
per-deleter boilerplate), you'd need to do something like:
std::unique_ptr<MyType, decltype(&myDeleter)> ptr(something, myDeleter);

This presents a few issues:

   1. You don't get to use the default constructor for unique_ptr. Not a 
   big deal but it can be inconvenient.
   2. You aren't actually declaring a unique_ptr that calls the myDeleter 
   function, you're declaring a unique_ptr that calls a function pointer with 
   the same signature as the myDeleter function. If the object gets passed 
   around a bit, the compiler won't be able to inline the deletion and you'll 
   probably be left with an indirect call.
   3. For the same reason as issue 2, storing the function pointer doubles 
   the size of the object.

I believe that:
std::unique_ptr<MyType, std::func_ptr<myDeleter>> ptr;
solves all of those issues.

-- 
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/e2229872-0c8a-4673-b452-602959fc938f%40isocpp.org.

------=_Part_23774_1996392239.1523288174476
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I propose the inclusion of a function object (somewhat sim=
ilar to the deprecated ptr_fun) that looks something like:<div><br><div><di=
v class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); bord=
er: 1px solid rgb(187, 187, 187); word-wrap: break-word;"><code class=3D"pr=
ettyprint"><div class=3D"subprettyprint" style=3D"caret-color: rgb(102, 0, =
102);"><span style=3D"color: #008;" class=3D"styled-by-prettify">template</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><=
span style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> f</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color:=
 #000;" class=3D"styled-by-prettify"><br>requires is_pointer_v</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">decltype</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">f</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">)&gt;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">&amp;&amp;</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0is_function_v</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify">remove_pointer_t</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span st=
yle=3D"color: #008;" class=3D"styled-by-prettify">decltype</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">f</span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">)&gt;&gt;</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">struct</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> func_ptr </span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"><br>=C2=A0 </span><span style=3D"color: #008;" class=3D"styled-b=
y-prettify">template</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">&lt;</span><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">typename</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">...</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span style=3D"color: #606;" class=3D"styled-by-prettify">Args</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 </span><span st=
yle=3D"color: #008;" class=3D"styled-by-prettify">decltype</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"colo=
r: #008;" class=3D"styled-by-prettify">auto</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">operator</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">()(</span><span style=3D"color: #606;" class=3D"styled-by-pret=
tify">Args</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
&amp;&amp;...</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> args</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> noexcept</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify">noexcept</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify">f</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">(</span><font color=3D"#000000"><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify">forward</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"co=
lor: #606;" class=3D"styled-by-prettify">Args</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">&gt;(</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify">args</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">)...</span></font><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">)))</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
<br>=C2=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">return</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> f</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify">forward</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span=
 style=3D"color: #606;" class=3D"styled-by-prettify">Args</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">&gt;(</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify">args</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">)...);</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"><br>=C2=A0 </span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">};</span></div></code></div><br>The main difference with =
ptr_fun being that the function pointer is part of the type.</div></div><di=
v><br></div><div><br></div><div><br></div><div><b><u>Motivation</u></b></di=
v><div><br></div><div>The main motivating example is a unique_ptr that need=
s to call a custom deleter function. Currently (assuming you don&#39;t want=
 to write some per-deleter boilerplate), you&#39;d need to do something lik=
e:</div><div><div class=3D"prettyprint" style=3D"background-color: rgb(250,=
 250, 250); border: 1px solid rgb(187, 187, 187); word-wrap: break-word;"><=
code class=3D"prettyprint"><div class=3D"subprettyprint" style=3D"caret-col=
or: rgb(102, 0, 102);"><span style=3D"color: #000;" class=3D"styled-by-pret=
tify">std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">:=
:</span><span style=3D"color: #000;" class=3D"styled-by-prettify">unique_pt=
r</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</spa=
n><span style=3D"color: #606;" class=3D"styled-by-prettify">MyType</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">decltype</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">(&amp;</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify">myDeleter</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">)&gt;</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> ptr</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">(</span><font color=3D"#000000"><span style=3D"color: #=
000;" class=3D"styled-by-prettify">something</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> myDeleter</span></font><span style=3D"color: #660=
;" class=3D"styled-by-prettify">);</span></div></code></div><br></div><div>=
This presents a few issues:</div><div><ol><li>You don&#39;t get to use the =
default constructor for unique_ptr. Not a big deal but it can be inconvenie=
nt.</li><li>You aren&#39;t actually declaring a unique_ptr that calls the m=
yDeleter function, you&#39;re declaring a unique_ptr that calls a function =
pointer with the same signature as the myDeleter function. If the object ge=
ts passed around a bit, the compiler won&#39;t be able to inline the deleti=
on and you&#39;ll probably be left with an indirect call.</li><li>For the s=
ame reason as issue 2, storing the function pointer doubles the size of the=
 object.</li></ol><div>I believe that:</div></div><div><div class=3D"pretty=
print" style=3D"background-color: rgb(250, 250, 250); border: 1px solid rgb=
(187, 187, 187); word-wrap: break-word;"><code class=3D"prettyprint"><div c=
lass=3D"subprettyprint" style=3D"caret-color: rgb(102, 0, 102);"><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify">std</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify">unique_ptr</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #606;" cl=
ass=3D"styled-by-prettify">MyType</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">,</span><font color=3D"#000000"><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"> std</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify">func_ptr</span><span style=3D"color: #080;" class=
=3D"styled-by-prettify">&lt;myDeleter&gt;</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> ptr</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">;</span></font></div></code></div>solves all of those is=
sues.</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/e2229872-0c8a-4673-b452-602959fc938f%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/e2229872-0c8a-4673-b452-602959fc938f=
%40isocpp.org</a>.<br />

------=_Part_23774_1996392239.1523288174476--

------=_Part_23773_924158111.1523288174475--

.
