220 18756 <CAOfiQqk2Fu8uMqd=BtgA+8tY2SiA3PRY8RLPZ6n6TPXqgnihNA@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: Richard Smith <richard@metafoo.co.uk>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Factory functions as deducing constructors? (N4471)
Date: Wed, 24 Jun 2015 16:59:06 -0700
Lines: 300
Approved: news@gmane.org
Message-ID: <CAOfiQqk2Fu8uMqd=BtgA+8tY2SiA3PRY8RLPZ6n6TPXqgnihNA@mail.gmail.com>
References: <C9C172D2-327C-47E6-B019-94D2FA0F3BF1@gmail.com>
	<9A3E0561DCF227429F72080EA81ADBC96077F5170F@TUS1XCHEVSPIN42.SYMC.SYMANTEC.COM>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=bcaec5489e45cbcbb305194c4a26
X-Trace: ger.gmane.org 1435190350 18690 80.91.229.3 (24 Jun 2015 23:59:10 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Wed, 24 Jun 2015 23:59:10 +0000 (UTC)
Cc: David Krauss <potswa@gmail.com>, "std-proposals@isocpp.org" <std-proposals@isocpp.org>
To: Michael Spertus <mike_spertus@symantec.com>
Original-X-From: std-proposals+bncBDVNBJG4YAIBBS4IVWWAKGQEC7BGQHA@isocpp.org Thu Jun 25 01:59:09 2015
Return-path: <std-proposals+bncBDVNBJG4YAIBBS4IVWWAKGQEC7BGQHA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-qc0-f200.google.com ([209.85.216.200])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDVNBJG4YAIBBS4IVWWAKGQEC7BGQHA@isocpp.org>)
	id 1Z7uZQ-0007Ix-OQ
	for gclcip-std-proposals@m.gmane.org; Thu, 25 Jun 2015 01:59:09 +0200
Original-Received: by qcfx5 with SMTP id x5sf42499859qcf.3
        for <gclcip-std-proposals@m.gmane.org>; Wed, 24 Jun 2015 16:59:07 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:sender:in-reply-to:references:date
         :message-id:subject:from:to:cc:content-type: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=v5YHw3Ho8AMMQezzSwT9rvKvbD9u+Z+JhiVrpTgRDR8=;
        b=CjX61N7Mslrw1mlmdOwlgtFXY1Rr1LlW5jwFfmn8I5gWI1yD9VZK81BNUaDBcsK1JG
         aOUGEJyaphDLHQDQ6yT1OlmpjkEHDDdrkTF1LLPLdOzQA+Kj0upy7LzDT7hRA8Cc+PEx
         cCNplgsuSdC7kA7mhv1RBvCTarj0MeXSHn6v9K2FS4lm7vc+vUyhAhpsGj1beYztAL8D
         VaGeS0aZQexmDBhsCDEbi7jXcOfFysZ0GK5y99qzmKCK6JU+7Q6WD6Pjj1zkjspd/gmS
         ySo7iZNVkBdSRUzOP2Z7PcF4DfywxT+GYDdX9mXpCIzZOvKn5YwiEJA+eQGzPJoG3Hd6
         Uu6Q== 
X-Gm-Message-State: ALoCoQkaQ/CKhrsfjbYRjGFQpuoK3FditrGquAEYxw23emulXcAZQZyaOkaEFRP+RuUdeslyOHbf
X-Received: by 10.52.28.5 with SMTP id x5mr56239925vdg.3.1435190347814;
        Wed, 24 Jun 2015 16:59:07 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.140.27.193 with SMTP id 59ls1009261qgx.22.gmail; Wed, 24 Jun
 2015 16:59:07 -0700 (PDT)
X-Received: by 10.52.136.239 with SMTP id qd15mr25652166vdb.89.1435190346934;
        Wed, 24 Jun 2015 16:59:06 -0700 (PDT)
Original-Received: from mail-vn0-x22f.google.com (mail-vn0-x22f.google.com. [2607:f8b0:400c:c0f::22f])
        by mx.google.com with ESMTPS id tt20si26362743vdb.78.2015.06.24.16.59.06
        for <std-proposals@isocpp.org>
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Wed, 24 Jun 2015 16:59:06 -0700 (PDT)
Received-SPF: pass (google.com: domain of metafoo@gmail.com designates 2607:f8b0:400c:c0f::22f as permitted sender) client-ip=2607:f8b0:400c:c0f::22f;
Original-Received: by vnbg129 with SMTP id g129so8683581vnb.2
        for <std-proposals@isocpp.org>; Wed, 24 Jun 2015 16:59:06 -0700 (PDT)
X-Received: by 10.52.114.230 with SMTP id jj6mr26368664vdb.66.1435190346630;
 Wed, 24 Jun 2015 16:59:06 -0700 (PDT)
Original-Sender: metafoo@gmail.com
Original-Received: by 10.31.75.2 with HTTP; Wed, 24 Jun 2015 16:59:06 -0700 (PDT)
In-Reply-To: <9A3E0561DCF227429F72080EA81ADBC96077F5170F@TUS1XCHEVSPIN42.SYMC.SYMANTEC.COM>
X-Original-Sender: richard@metafoo.co.uk
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of metafoo@gmail.com designates 2607:f8b0:400c:c0f::22f as permitted
 sender) smtp.mail=metafoo@gmail.com;       dkim=pass header.i=@gmail.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: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://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>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:18756
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/18756>

--bcaec5489e45cbcbb305194c4a26
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On Wed, Jun 24, 2015 at 2:55 PM, Michael Spertus <mike_spertus@symantec.com=
>
wrote:

> Hi David,
>
> That sounds to me essentially like the factory functions part of the
> proposal except you allow bodies in them. Is that correct? We explicitly
> rejected that because I felt that the body belonged in the class and was
> consistent with C++=E2=80=99 traditional values about what should be in t=
he class
> definition (Although I can=E2=80=99t speak for Richard=E2=80=99s motivati=
on, he also
> explicitly forbid defining the body).
>

I arrived at the current "guiding declarations" approach as a special case
of a more general mechanism. Sometimes you want a function template that
has some non-trivial way of computing its desired parameter types from the
types of its arguments. This can't be done in today's C++: if you capture
your argument types through deduction, your parameter types must exactly
match the argument types. So the feature I was considering would allow this=
:

  template<typename ...Args> void f_impl(maybe_add_ref<Args>...);
  template<typename ...Args> some_keyword_maybe f(Args...) ->
f_impl<Args...>;

The second declaration is a deduction guide: it says, when you see a call
f(blah), and that function is the best viable function, then proceed as if
f were replaced by f_impl<Args...>. And for constructors:

  template<typename T> some_keyword_maybe vector(initializer_list<T>) ->
vector<T>;

.... says essentially the same thing: when you see vector{1, 2, 3}, if that
declaration is the best viable function (say, with T =3D int), then 'vector=
'
really means 'vector<int>'.

One reason why you would want something like this instead of giving a
definition to the constructor (or function template) is to avoid
duplication -- you already have the constructor definition, and shouldn't
be repeating it in the way the old make_-function pattern forced you to.
Another reason is to avoid unnecessary copies / moves of function arguments=
..


>  A point in favor of your approach is that Bjarne=E2=80=99s unified funct=
ion call
> syntax proposal is to support =E2=80=9Cexternally defined methods=E2=80=
=9D but doesn=E2=80=99t work
> for constructors.
>
>
>
> I plan to start looking at the next revision in about a month. OK if we
> discuss then?
>
>
>
> Thanks,
>
>
>
> Mike
>
>
>
> *From:* David Krauss [mailto:potswa@gmail.com]
> *Sent:* Wednesday, June 24, 2015 4:45 AM
> *To:* Michael Spertus; std-proposals@isocpp.org
> *Cc:* Richard Smith
> *Subject:* Factory functions as deducing constructors? (N4471)
>
>
>
> Hi Mike,
>
>
>
> Most libraries already implement factory function templates, e.g.
> make_thing(). Why not just let them be named e.g. thing(), aliasing the
> class template?
>
>
>
>    - No new deduction rules are needed.
>    - It=E2=80=99s a non-breaking change because such aliasing is currentl=
y
>    disallowed.
>    - It differs from established practice only by renaming.
>    - The full flexibility of factory functions is preserved, including
>    explicit template arguments.
>    - It does not intrude into the class template implementation.
>
>
>
> For the sake of member templates, such a function template (or function)
> should still be found by a qualified-id preceded by the typename keyword.
> It should also be illegal to take its address, or to do anything with its
> name or template-id besides call it. The factory should defer to real
> constructors when the explicit template argument list satisfies the class
> template parameters.
>
>
>
> Require the factory to be declared after the initial declaration of the
> class template, and require its return type to be a specialization.
>
>
>
> Implementation model: Attach the overload set of factory functions to the
> class template. When name lookup finds the class template, grab the
> factories too. If template arguments are provided, treat the class templa=
te
> as an overload to the factories and run template argument deduction for
> explicit arguments ([temp.deduct]/1-5). If the class template has not
> suffered a deduction failure, terminate deduction and restart overload
> resolution using its constructors.
>
>
>
> I=E2=80=99d also like to propose enforced copy elision, such that simple =
factory
> functions don=E2=80=99t require move construction. (Call it =E2=80=9Cperf=
ect return.=E2=80=9D) This
> seems like a separate feature. However, it would be nice to have both.
>
>
>
> Long ago, in some early C++ implementations at least, constructors *were*=
 non-member
> factory functions. Hopefully this won=E2=80=99t be seen as =E2=80=9Ccomin=
g full circle.=E2=80=9D
> It=E2=80=99s really not, since the factory has no special abilities or st=
atus
> besides sharing a name.
>
>
>
>             - D
>
>
>

--=20

---=20
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 e=
mail 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-proposa=
ls/.

--bcaec5489e45cbcbb305194c4a26
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On W=
ed, Jun 24, 2015 at 2:55 PM, Michael Spertus <span dir=3D"ltr">&lt;<a href=
=3D"mailto:mike_spertus@symantec.com" target=3D"_blank">mike_spertus@symant=
ec.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"><div lang=3D=
"EN-US" link=3D"#0563C1" vlink=3D"#954F72"><div><p class=3D"MsoNormal"><spa=
n style=3D"font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif;colo=
r:#1f497d">Hi David,<u></u><u></u></span></p><p class=3D"MsoNormal"><span s=
tyle=3D"font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif;color:#=
1f497d">That sounds to me essentially like the factory functions part of th=
e proposal except you allow bodies in them. Is that correct? We explicitly =
rejected that because I felt that the body belonged in the class and was co=
nsistent with C++=E2=80=99 traditional values about what should be in the c=
lass definition (Although I can=E2=80=99t speak for Richard=E2=80=99s motiv=
ation, he also explicitly forbid defining the body).</span></p></div></div>=
</blockquote><div><br></div><div>I arrived at the current &quot;guiding dec=
larations&quot; approach as a special case of a more general mechanism. Som=
etimes you want a function template that has some non-trivial way of comput=
ing its desired parameter types from the types of its arguments. This can&#=
39;t be done in today&#39;s C++: if you capture your argument types through=
 deduction, your parameter types must exactly match the argument types. So =
the feature I was considering would allow this:</div><div><br></div><div>=
=C2=A0 template&lt;typename ...Args&gt; void f_impl(maybe_add_ref&lt;Args&g=
t;...);</div><div>=C2=A0 template&lt;typename ...Args&gt; some_keyword_mayb=
e f(Args...) -&gt; f_impl&lt;Args...&gt;;</div><div><br></div><div>The seco=
nd declaration is a deduction guide: it says, when you see a call f(blah), =
and that function is the best viable function, then proceed as if f were re=
placed by f_impl&lt;Args...&gt;. And for constructors:</div><div><br></div>=
<div>=C2=A0 template&lt;typename T&gt; some_keyword_maybe vector(initialize=
r_list&lt;T&gt;) -&gt; vector&lt;T&gt;;</div><div><br></div><div>... says e=
ssentially the same thing: when you see vector{1, 2, 3}, if that declaratio=
n is the best viable function (say, with T =3D int), then &#39;vector&#39; =
really means &#39;vector&lt;int&gt;&#39;.</div><div><br></div><div>One reas=
on why you would want something like this instead of giving a definition to=
 the constructor (or function template) is to avoid duplication -- you alre=
ady have the constructor definition, and shouldn&#39;t be repeating it in t=
he way the old make_-function pattern forced you to. Another reason is to a=
void unnecessary copies / moves of function arguments.</div><div>=C2=A0</di=
v><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:=
1px #ccc solid;padding-left:1ex"><div lang=3D"EN-US" link=3D"#0563C1" vlink=
=3D"#954F72"><div><p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;fo=
nt-family:&quot;Calibri&quot;,sans-serif;color:#1f497d"> =C2=A0A point in f=
avor of your approach is that Bjarne=E2=80=99s unified function call syntax=
 proposal is to support =E2=80=9Cexternally defined methods=E2=80=9D but do=
esn=E2=80=99t work for constructors.<u></u><u></u></span></p><p class=3D"Ms=
oNormal"><span style=3D"font-size:11.0pt;font-family:&quot;Calibri&quot;,sa=
ns-serif;color:#1f497d"><u></u>=C2=A0<u></u></span></p><p class=3D"MsoNorma=
l"><span style=3D"font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-ser=
if;color:#1f497d">I plan to start looking at the next revision in about a m=
onth. OK if we discuss then?<u></u><u></u></span></p><p class=3D"MsoNormal"=
><span style=3D"font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif=
;color:#1f497d"><u></u>=C2=A0<u></u></span></p><p class=3D"MsoNormal"><span=
 style=3D"font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif;color=
:#1f497d">Thanks,<u></u><u></u></span></p><p class=3D"MsoNormal"><span styl=
e=3D"font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif;color:#1f4=
97d"><u></u>=C2=A0<u></u></span></p><p class=3D"MsoNormal"><span style=3D"f=
ont-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif;color:#1f497d">M=
ike<u></u><u></u></span></p><p class=3D"MsoNormal"><span style=3D"font-size=
:11.0pt;font-family:&quot;Calibri&quot;,sans-serif;color:#1f497d"><u></u>=
=C2=A0<u></u></span></p><div><div style=3D"border:none;border-top:solid #e1=
e1e1 1.0pt;padding:3.0pt 0in 0in 0in"><p class=3D"MsoNormal"><b><span style=
=3D"font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">From:</spa=
n></b><span style=3D"font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-=
serif"> David Krauss [mailto:<a href=3D"mailto:potswa@gmail.com" target=3D"=
_blank">potswa@gmail.com</a>] <br><b>Sent:</b> Wednesday, June 24, 2015 4:4=
5 AM<br><b>To:</b> Michael Spertus; <a href=3D"mailto:std-proposals@isocpp.=
org" target=3D"_blank">std-proposals@isocpp.org</a><br><b>Cc:</b> Richard S=
mith<br><b>Subject:</b> Factory functions as deducing constructors? (N4471)=
<u></u><u></u></span></p></div></div><div><div class=3D"h5"><p class=3D"Mso=
Normal"><u></u>=C2=A0<u></u></p><div><p class=3D"MsoNormal">Hi Mike,<u></u>=
<u></u></p></div><div><p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p></div>=
<div><p class=3D"MsoNormal">Most libraries already implement factory functi=
on templates, e.g. <span style=3D"font-family:Courier">make_thing()</span>.=
 Why not just let them be named e.g.=C2=A0<span style=3D"font-family:Courie=
r">thing()</span>, aliasing the class template?<u></u><u></u></p></div><div=
><p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p></div><div><ul type=3D"disc=
"><li class=3D"MsoNormal">No new deduction rules are needed.<u></u><u></u><=
/li><li class=3D"MsoNormal">It=E2=80=99s a non-breaking change because such=
 aliasing is currently disallowed.<u></u><u></u></li><li class=3D"MsoNormal=
">It differs from established practice only by renaming.<u></u><u></u></li>=
<li class=3D"MsoNormal">The full flexibility of factory functions is preser=
ved, including explicit template arguments.<u></u><u></u></li><li class=3D"=
MsoNormal">It does not intrude into the class template implementation.<u></=
u><u></u></li></ul></div><div><p class=3D"MsoNormal"><u></u>=C2=A0<u></u></=
p></div><div><p class=3D"MsoNormal">For the sake of member templates, such =
a function template (or function) should still be found by a qualified-id p=
receded by the <span style=3D"font-family:Courier">typename</span> keyword.=
 It should also be illegal to take its address, or to do anything with its =
name or template-id besides call it. The factory should defer to real const=
ructors when the explicit template argument list satisfies the class templa=
te parameters.<u></u><u></u></p></div><div><p class=3D"MsoNormal"><u></u>=
=C2=A0<u></u></p></div><div><p class=3D"MsoNormal">Require the factory to b=
e declared after the initial declaration of the class template, and require=
 its return type to be a specialization.<u></u><u></u></p></div><div><p cla=
ss=3D"MsoNormal"><u></u>=C2=A0<u></u></p></div><div><p class=3D"MsoNormal">=
Implementation model: Attach the overload set of factory functions to the c=
lass template. When name lookup finds the class template, grab the factorie=
s too. If template arguments are provided, treat the class template as an o=
verload to the factories and run template argument deduction for explicit a=
rguments ([temp.deduct]/1-5). If the class template has not suffered a dedu=
ction failure, terminate deduction and restart overload resolution using it=
s constructors.<u></u><u></u></p></div><div><p class=3D"MsoNormal"><u></u>=
=C2=A0<u></u></p></div><div><p class=3D"MsoNormal">I=E2=80=99d also like to=
 propose enforced copy elision, such that simple factory functions don=E2=
=80=99t require move construction. (Call it =E2=80=9Cperfect return.=E2=80=
=9D) This seems like a separate feature. However, it would be nice to have =
both.<u></u><u></u></p></div><div><p class=3D"MsoNormal"><u></u>=C2=A0<u></=
u></p></div><div><p class=3D"MsoNormal">Long ago, in some early C++ impleme=
ntations at least, constructors=C2=A0<i>were</i>=C2=A0non-member factory fu=
nctions. Hopefully this won=E2=80=99t be seen as =E2=80=9Ccoming full circl=
e.=E2=80=9D It=E2=80=99s really not, since the factory has no special abili=
ties or status besides sharing a name.<u></u><u></u></p></div><div><p class=
=3D"MsoNormal"><u></u>=C2=A0<u></u></p></div><div><p class=3D"MsoNormal"><s=
pan>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 </sp=
an>- D<u></u><u></u></p></div><div><p class=3D"MsoNormal"><u></u>=C2=A0<u><=
/u></p></div></div></div></div></div></blockquote></div><br></div></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 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 />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--bcaec5489e45cbcbb305194c4a26--

.
