220 19870 <86717349-1fce-4247-9372-08eedce4cd30@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: Arthur O'Dwyer <arthur.j.odwyer@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: default arguments
Date: Mon, 17 Aug 2015 15:52:24 -0700 (PDT)
Lines: 294
Approved: news@gmane.org
Message-ID: <86717349-1fce-4247-9372-08eedce4cd30@isocpp.org>
References: <ef9c7cbe-e8f1-4196-83bc-11a1c9b2798c@isocpp.org>
 <736eb378-52a4-43ba-b3b5-a73719ba70af@isocpp.org>
 <mqtask$51j$1@ger.gmane.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_1740_1780373017.1439851944288"
X-Trace: ger.gmane.org 1439851948 31387 80.91.229.3 (17 Aug 2015 22:52:28 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Mon, 17 Aug 2015 22:52:28 +0000 (UTC)
Cc: bop@gmb.dk
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBDLZJYWNDQIKTS6JVYCRUBGV33VLU@isocpp.org Tue Aug 18 00:52:27 2015
Return-path: <std-proposals+bncBDLZJYWNDQIKTS6JVYCRUBGV33VLU@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pa0-f71.google.com ([209.85.220.71])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDLZJYWNDQIKTS6JVYCRUBGV33VLU@isocpp.org>)
	id 1ZRTGV-00035J-4J
	for gclcip-std-proposals@m.gmane.org; Tue, 18 Aug 2015 00:52:27 +0200
Original-Received: by paom9 with SMTP id m9sf344974721pao.3
        for <gclcip-std-proposals@m.gmane.org>; Mon, 17 Aug 2015 15:52:26 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to:cc:message-id:in-reply-to:references:subject
         :mime-version:content-type: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=CoOp5NhzBCUHez4enT/qRG9+jt56i0HS0s37LV7qZV0=;
        b=HpxtWnLCnGyu5u5seEfkDhmozj/zNXKa0Vpxs1Fn7PzzZHOm+QTOv2SbMt4H4oxb0Z
         hSA7/yaewoE9v7T0bOD+SZ892vcpvejVIPOLAy34yEvcvnuhhLnhpKt0HgTIZlRsyZdw
         JxzpjruKQwX1Zt6z9MEYSnmPY9tfjEXfmxVAFz4QLO1dlvqVGz/ZUIZCGTNw0t+Hko+U
         rhB4WCOoHhgiR3EWohexcaHsJgBg8Lt089fYKIQe9bapSgQtofK9yJZA38ICpjAAmk82
         GcTUmXTRVE5l2SIQIBVjXsoKnn3g8IKDdEkWYP08xJHWedLOdAKwBE30dPfvRZsk31m+
         OnKg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:date:from:to:cc:message-id:in-reply-to
         :references:subject:mime-version:content-type: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=CoOp5NhzBCUHez4enT/qRG9+jt56i0HS0s37LV7qZV0=;
        b=Iu0B9bwfHSoN7yQM0V+mrNbsfYFnAvqf5sg/UY4PCHPzdcd+XcUbX0KDV2KtbdtbLT
         n8no3sOjJ1L1MVSD5C7GfSrNxAzWE2GLHrMUjbTFrhkY0WlcBYmnkAdXAH0+h+kMQ+PR
         BypkHiMXW84u619BWp68bU4jgRJL2izLdRCgjkSyTiLsogw3pkDJarsw0oZR9tvtlVl7
         zsngy3t+FaYmZJzCf+gWZH0Z/3zRaqmSyLH2KAcGC5TiDymYPPeBA+uEJsNKmJdCqmMY
         HoWpcDn3y3cZjZjub+PzUYmFPduk9MUJxkYr1woRNAkuOS++kUbtrK4k+H76tBA4RdCN
         l0TA==
X-Gm-Message-State: ALoCoQlbyRxkmrtaWwM5R33EIW06cTZMII+GbPNVJjYwfInKbpOwQQTPdwrcs/ai00nQNL7eepTd
X-Received: by 10.66.237.69 with SMTP id va5mr3170865pac.48.1439851946087;
        Mon, 17 Aug 2015 15:52:26 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.107.39.140 with SMTP id n134ls1899046ion.51.gmail; Mon, 17 Aug
 2015 15:52:25 -0700 (PDT)
X-Received: by 10.50.137.7 with SMTP id qe7mr285217igb.17.1439851945305;
        Mon, 17 Aug 2015 15:52:25 -0700 (PDT)
In-Reply-To: <mqtask$51j$1@ger.gmane.org>
X-Original-Sender: arthur.j.odwyer@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:19870
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/19870>

------=_Part_1740_1780373017.1439851944288
Content-Type: multipart/alternative; 
	boundary="----=_Part_1741_413134508.1439851944288"

------=_Part_1741_413134508.1439851944288
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

I work on a codebase where this feature would have lots of use-cases,=20
because we have lots of classes whose constructors have defaulted=20
parameters:

    C(int buffer_size =3D 1024, int timeout =3D 10, int tries =3D 3) :=20
buffer_size_(buffer_size), timeout_(timeout), tries_(tries) {}

    auto cptr =3D new C();  // usually the defaults are okay
    auto c2ptr =3D new C(2048);  // use a bigger buffer
    auto c3ptr =3D new C(1024, 1);  // use the default buffer size but with=
 a=20
shorter timeout
    auto c4ptr =3D new C(2048, 10, 5);  // testing: bump up the number of=
=20
tries

It would be marginally more convenient (and refactor-proof) to be able to=
=20
write

    auto cptr =3D new C();  // usually the defaults are okay
    auto c2ptr =3D new C(2048);  // use a bigger buffer
    auto c3ptr =3D new C(default, 1);  // use the default buffer size but=
=20
with a shorter timeout
    auto c4ptr =3D new C(2048, default, 5);  // testing: bump up the number=
=20
of tries

However, I consider defaulted parameters in general to be a huge=20
antipattern, and this constructor pattern in particular to be even worse.
I would MUCH rather just rewrite all these constructors so that they could=
=20
be used as

    auto cptr =3D new C();  // usually the defaults are okay
    auto c2ptr =3D new C().withBufferSize(2048);  // use a bigger buffer
    auto c3ptr =3D new C().withTimeout(1);  // use the default buffer size=
=20
but with a shorter timeout
    auto c4ptr =3D new C().withBufferSize(2048).withTries(5);  // testing:=
=20
bump up the number of tries

or

    auto cptr =3D new C();  // usually the defaults are okay
    auto c2ptr =3D new C(C::BufferSize(2048));  // use a bigger buffer
    auto c3ptr =3D new C(C::Timeout(1));  // use the default buffer size bu=
t=20
with a shorter timeout
    auto c4ptr =3D new C(C::BufferSize(2048), C::Tries(5));  // testing: bu=
mp=20
up the number of tries

or (the most conservative way to rewrite our existing code)

    static constexpr int default_buffer_size =3D 1024;
    static constexpr int default_timeout =3D 10;
    static constexpr int default_tries =3D 3;
    C() : buffer_size_(default_buffer_size), timeout_(default_timeout),=20
tries_(default_tries) {}
    C(int buffer_size, int timeout, int tries) : buffer_size_(buffer_size),=
=20
timeout_(timeout), tries_(tries) {}

    auto cptr =3D new C();  // usually the defaults are okay
    auto c2ptr =3D new C(2048, C::default_timeout, C::default_tries);  // u=
se=20
a bigger buffer
    auto c3ptr =3D new C(C::default_buffer_size, 1, C::default_tries);  //=
=20
use the default buffer size but with a shorter timeout
    auto c4ptr =3D new C(2048, C::default_timeout, 5);  // testing: bump up=
=20
the number of tries

I don't really want to make it *easier* for people to write the "function=
=20
taking tons of defaulted parameters" antipattern.
Let's teach people how to avoid the use of defaulted parameters, instead.

=E2=80=93Arthur


On Monday, August 17, 2015 at 11:58:41 AM UTC-7, Bo Persson wrote:
>
> On 2015-08-17 20:37, Arthur Tchaikovsky wrote:=20
> > I would really appreciate some feedback from people, or is this proposa=
l=20
> > so uninteresting that there is no point in wasting keyboard on it?=20
>
> You might wonder how widely used it will be?=20
>
> If not all parameters are of the same type, you can already get some of=
=20
> the effect with a few overloads:=20
>
>      void f(int a =3D 0, string b =3D "1",=20
>             complex<double> c =3D 2.0, type4 d =3D 3);=20
>
>      inline void f(int a, complex<double> c)=20
>      { f(a, "1", c, 3); }=20
>
>      inline void f(string b, complex<double> c)=20
>      { f(0, b, c, 3); }=20
>
> assuming that d is put last because you hardly ever want to change the=20
> default.=20
>
> Just saying that you can already do this without a language change.=20
>
>
>     Bo Persson=20
>
>
>
> >=20
> > On Saturday, 15 August 2015 17:49:50 UTC+1, Arthur Tchaikovsky wrote:=
=20
> >=20
> >     Hi,=20
> >     Does anyone see helpfulness of such solution:=20
> >     Having fnc:=20
> >=20
> >     void f(int a =3D 0, int b =3D 1, int c =3D 2, int d =3D 3);=20
> >=20
> >     I think that it would be nice to be able to say whilst calling it=
=20
> >     and requiring only some of those args to be non-default:=20
> >=20
> >     f(default,2,default,4);=20
> >=20
> >     Thoughts?=20
> >=20
> >=20
>
>
>

--=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/.

------=_Part_1741_413134508.1439851944288
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I work on a codebase where this feature would have lots of=
 use-cases, because we have lots of classes whose constructors have default=
ed parameters:<div><br></div><div>=C2=A0 =C2=A0 C(int buffer_size =3D 1024,=
 int timeout =3D 10, int tries =3D 3) : buffer_size_(buffer_size), timeout_=
(timeout), tries_(tries) {}</div><div><br></div><div>=C2=A0 =C2=A0 auto cpt=
r =3D new C(); =C2=A0// usually the defaults are okay</div><div>=C2=A0 =C2=
=A0 auto c2ptr =3D new C(2048); =C2=A0// use a bigger buffer<br>=C2=A0 =C2=
=A0 auto c3ptr =3D new C(1024, 1); =C2=A0// use the default buffer size but=
 with a shorter timeout</div><div>=C2=A0 =C2=A0 auto c4ptr =3D new C(2048, =
10, 5); =C2=A0// testing: bump up the number of tries</div><div><br></div><=
div>It would be marginally more convenient (and refactor-proof) to be able =
to write</div><div><br></div><div><div>=C2=A0 =C2=A0 auto cptr =3D new C();=
 =C2=A0// usually the defaults are okay</div><div>=C2=A0 =C2=A0 auto c2ptr =
=3D new C(2048); =C2=A0// use a bigger buffer<br>=C2=A0 =C2=A0 auto c3ptr =
=3D new C(default, 1); =C2=A0// use the default buffer size but with a shor=
ter timeout</div><div>=C2=A0 =C2=A0 auto c4ptr =3D new C(2048,=C2=A0default=
, 5); =C2=A0// testing: bump up the number of tries</div><div><br></div><di=
v>However, I consider defaulted parameters in general to be a huge antipatt=
ern, and this constructor pattern in particular to be even worse.</div><div=
>I would MUCH rather just rewrite all these constructors so that they could=
 be used as</div><div><br></div><div>=C2=A0 =C2=A0 auto cptr =3D new C(); =
=C2=A0// usually the defaults are okay</div><div>=C2=A0 =C2=A0 auto c2ptr =
=3D new C().withBufferSize(2048); =C2=A0// use a bigger buffer<br>=C2=A0 =
=C2=A0 auto c3ptr =3D new C().withTimeout(1); =C2=A0// use the default buff=
er size but with a shorter timeout</div><div>=C2=A0 =C2=A0 auto c4ptr =3D n=
ew C().withBufferSize(2048).withTries(5); =C2=A0// testing: bump up the num=
ber of tries</div><div><br></div><div>or</div><div><br></div><div><div>=C2=
=A0 =C2=A0 auto cptr =3D new C(); =C2=A0// usually the defaults are okay</d=
iv><div>=C2=A0 =C2=A0 auto c2ptr =3D new C(C::BufferSize(2048)); =C2=A0// u=
se a bigger buffer<br>=C2=A0 =C2=A0 auto c3ptr =3D new C(C::Timeout(1)); =
=C2=A0// use the default buffer size but with a shorter timeout</div><div>=
=C2=A0 =C2=A0 auto c4ptr =3D new C(C::BufferSize(2048), C::Tries(5)); =C2=
=A0// testing: bump up the number of tries</div></div><div><br></div><div>o=
r (the most conservative way to rewrite our existing code)</div><div><br></=
div><div>=C2=A0 =C2=A0 static constexpr int default_buffer_size =3D 1024;</=
div><div>=C2=A0 =C2=A0 static constexpr int default_timeout =3D 10;</div><d=
iv></div><div>=C2=A0 =C2=A0 static constexpr int default_tries =3D 3;</div>=
<div></div><div>=C2=A0 =C2=A0 C() : buffer_size_(default_buffer_size), time=
out_(default_timeout), tries_(default_tries) {}<br></div><div><div><div>=C2=
=A0 =C2=A0 C(int buffer_size, int timeout, int tries) : buffer_size_(buffer=
_size), timeout_(timeout), tries_(tries) {}</div><div></div></div></div><di=
v><br></div><div>=C2=A0 =C2=A0 auto cptr =3D new C(); =C2=A0// usually the =
defaults are okay<br></div><div><div>=C2=A0 =C2=A0 auto c2ptr =3D new C(204=
8, C::default_timeout, C::default_tries); =C2=A0// use a bigger buffer<br>=
=C2=A0 =C2=A0 auto c3ptr =3D new C(C::default_buffer_size, 1, C::default_tr=
ies); =C2=A0// use the default buffer size but with a shorter timeout</div>=
<div>=C2=A0 =C2=A0 auto c4ptr =3D new C(2048, C::default_timeout, 5); =C2=
=A0// testing: bump up the number of tries</div></div><div><br></div><div>I=
 don&#39;t really want to make it <i>easier</i> for people to write the &qu=
ot;function taking tons of defaulted parameters&quot; antipattern.</div><di=
v>Let&#39;s teach people how to avoid the use of defaulted parameters, inst=
ead.</div><div><br></div><div>=E2=80=93Arthur</div><div><br></div><div><br>=
</div>On Monday, August 17, 2015 at 11:58:41 AM UTC-7, Bo Persson wrote:<bl=
ockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border=
-left: 1px #ccc solid;padding-left: 1ex;">On 2015-08-17 20:37, Arthur Tchai=
kovsky wrote:
<br>&gt; I would really appreciate some feedback from people, or is this pr=
oposal
<br>&gt; so uninteresting that there is no point in wasting keyboard on it?
<br>
<br>You might wonder how widely used it will be?
<br>
<br>If not all parameters are of the same type, you can already get some of=
=20
<br>the effect with a few overloads:
<br>
<br>=C2=A0 =C2=A0 =C2=A0void f(int a =3D 0, string b =3D &quot;1&quot;,
<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 complex&lt;double&gt; c =3D 2=
..0, type4 d =3D 3);
<br>
<br>=C2=A0 =C2=A0 =C2=A0inline void f(int a, complex&lt;double&gt; c)
<br>=C2=A0 =C2=A0 =C2=A0{ f(a, &quot;1&quot;, c, 3); }
<br>
<br>=C2=A0 =C2=A0 =C2=A0inline void f(string b, complex&lt;double&gt; c)
<br>=C2=A0 =C2=A0 =C2=A0{ f(0, b, c, 3); }
<br>
<br>assuming that d is put last because you hardly ever want to change the=
=20
<br>default.
<br>
<br>Just saying that you can already do this without a language change.
<br>
<br>
<br>=C2=A0 =C2=A0 Bo Persson
<br>
<br>
<br>
<br>&gt;
<br>&gt; On Saturday, 15 August 2015 17:49:50 UTC+1, Arthur Tchaikovsky wro=
te:
<br>&gt;
<br>&gt; =C2=A0 =C2=A0 Hi,
<br>&gt; =C2=A0 =C2=A0 Does anyone see helpfulness of such solution:
<br>&gt; =C2=A0 =C2=A0 Having fnc:
<br>&gt;
<br>&gt; =C2=A0 =C2=A0 void f(int a =3D 0, int b =3D 1, int c =3D 2, int d =
=3D 3);
<br>&gt;
<br>&gt; =C2=A0 =C2=A0 I think that it would be nice to be able to say whil=
st calling it
<br>&gt; =C2=A0 =C2=A0 and requiring only some of those args to be non-defa=
ult:
<br>&gt;
<br>&gt; =C2=A0 =C2=A0 f(default,2,default,4);
<br>&gt;
<br>&gt; =C2=A0 =C2=A0 Thoughts?
<br>&gt;
<br>&gt;
<br>
<br>
<br></blockquote></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 />

------=_Part_1741_413134508.1439851944288--
------=_Part_1740_1780373017.1439851944288--

.
