220 34491 <CAOfiQqnmBJ+H2a1R6pCeKkJqBYmOX3qK0ZFyNRMGngStSSChVA@mail.gmail.com> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Richard Smith <richard@metafoo.co.uk>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Auto return type deduction surprises
Date: Tue, 26 Sep 2017 01:53:57 -0700
Lines: 180
Approved: news@gmane.org
Message-ID: <CAOfiQqnmBJ+H2a1R6pCeKkJqBYmOX3qK0ZFyNRMGngStSSChVA@mail.gmail.com>
References: <7bf0d2563477e89a8faa3ca2daa18f67@kdab.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="001a113de226017dca055a13d248"
X-Trace: blaine.gmane.org 1506416061 27278 195.159.176.226 (26 Sep 2017 08:54:21 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Tue, 26 Sep 2017 08:54:21 +0000 (UTC)
To: "std-proposals@isocpp.org" <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBDVNBJG4YAIBBO5LVDHAKGQEXFEHKQQ@isocpp.org Tue Sep 26 10:54:16 2017
Return-path: <std-proposals+bncBDVNBJG4YAIBBO5LVDHAKGQEXFEHKQQ@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-wr0-f200.google.com ([209.85.128.200])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDVNBJG4YAIBBO5LVDHAKGQEXFEHKQQ@isocpp.org>)
	id 1dwld7-0006Ws-3l
	for gclcip-std-proposals@m.gmane.org; Tue, 26 Sep 2017 10:54:13 +0200
Original-Received: by mail-wr0-f200.google.com with SMTP id u48sf1733648wrf.19
        for <gclcip-std-proposals@m.gmane.org>; Tue, 26 Sep 2017 01:54:20 -0700 (PDT)
ARC-Seal: i=2; a=rsa-sha256; t=1506416060; cv=pass;
        d=google.com; s=arc-20160816;
        b=OXns8V6th2bQ5+91TwbOorFpkIE54AEA0JXtjVGWhI/NGb53B5MFhNOPJs85fdNrDR
         Zr6Iz3lPpK1W0I5+1EKWzpr4M7JCvIJi86p1uN9B7IWjNC9eBkMSkI70Vw4FfanJwa7b
         TuqHAdI7TtswSrjAMCbOeo7c0WbXRKiGC2br0620gmBlOIWN7CTo5gv9ZtVWilUxKY5L
         /b7BMbdRceHOTYWupsTgpwQbUpYmrkFaXot2FYXB66qng6ajGu7jK57VuVUt6EbQcCMz
         pzDfWF5xNWHRpdJIpmBATanNfMXFglxkj0N0Nn3Yhju2tY6pqHEqHQ7s9ldoifY+PCoH
         BsfQ==
ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post
         :list-id:mailing-list:precedence:reply-to:to:subject:message-id:date
         :from:references:in-reply-to:mime-version:arc-authentication-results
         :arc-message-signature:dkim-signature:arc-authentication-results;
        bh=GuQM3rr4vHFgZoqeq+a2XAMz+VcNXaQ2Zu5CVrIJ6fQ=;
        b=A8XyKrurFPpliuvRwiPEwVYawMIHjDaSXGckYaC4zP7iPT4/eeLJrK6VVZBbHxsHgK
         MfFKv/TbWUwZSOuzpCBTB6pgZdssZismIAuTjBc58u9Fq5/EChcBpFoA/AUHcxUo01jM
         28lBCm9kcJvjdmTRmIeSamcEx2aotyH5y5sx6708uStfxnXGPWZKoRpqMIboGZklcC+w
         CN0PFnMR52VZNGZL6wLNOFdm+sTejjS19bAJbyKmZ++22D/jBnYrw6FY3a8rFtaEAkLN
         SNSFlhhDOt0U2e3XC/EDmoR5tIe70/eceYldJrOanvGIb4cflsuZxrAVKEVoNQ65EpSK
         9yOg==
ARC-Authentication-Results: i=2; mx.google.com;
       dkim=pass header.i=@metafoo.co.uk header.s=default header.b=YPaUH7CV;
       spf=neutral (google.com: 81.27.85.19 is neither permitted nor denied by best guess record for domain of richard@metafoo.co.uk) smtp.mailfrom=richard@metafoo.co.uk
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:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=GuQM3rr4vHFgZoqeq+a2XAMz+VcNXaQ2Zu5CVrIJ6fQ=;
        b=TmZdBRzsBj5Gz8fBCoTM3TB47+lFNLBYXOIfAUvRDi5Y2W+cAmvw6SEeZ+gax8Kxm2
         s8p1RGcF+61aLf04qd2QP9gB8q4gQNMSJ+vuC6aot8hG5HFM7x89pcAjXxos++VFLaGx
         nHFtAt8AjtXGwUwCIj8fiULbfljWTtM6hr/WB8R05MTWWKX8ymOrEKfyOr70AG5Hyrhr
         s8n5uRtNUF2UvI1T6zyO6dFXW3T2LAk5TgjvW5LMB7r9YC6GGtTr5DCW//OR3TlHn1Os
         LmuDxXMtt70CeWsjwZDlqO3k8F9AOTp82U9itiuQg6Wnjj/sm80FjmYaRDPBY2MOSTaa
         0Jug==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        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=GuQM3rr4vHFgZoqeq+a2XAMz+VcNXaQ2Zu5CVrIJ6fQ=;
        b=Y4csGCCpBlG9G+t9HdLmbuuOb+Fy4XCFAhwfTejPQrFeQmaqBAJij3bbChG2t/YktX
         /LxpYLqlotikMTKGZrdPVGIJDSPCjbVEarj4L9SgRRsfTgQYfUd8UaVZ5vh0MedAWasZ
         BeDoeHOAdWDt9zDTGuSvXfxncCQLKdXKIcwF61Rf1p2jP6jQO6j5O8aQ3jfUMYbbCkkd
         BVulA/OUCNMWQW+jTgonuBcYWWEcvWSdo8d75d/+G3fmAo2J0M2ZcHznzeHyAr/h3zk3
         LHJ/S/OxFv1oL465luiOcipsMgiOryftz8i024RpsENgkWMUOyvpLy3Uh9+5ME6NZBY2
         aPAQ==
X-Gm-Message-State: AHPjjUi3WAHsrdVmMzHhb4oBlaNXjnfnjaCwJKoTUT4usk9DAcaQOCKf
	Fs4X+F59Db6R5NPahrpgOoFn9w==
X-Google-Smtp-Source: AOwi7QCc1NfkKcDxjATV4YKh0XbD1fKCJjp14BjyiP6bSrZPSwPowyqq0Eg60QEdusvplbVVslydsQ==
X-Received: by 10.25.163.210 with SMTP id m201mr772625lfe.42.1506416060456;
        Tue, 26 Sep 2017 01:54:20 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.28.184.83 with SMTP id i80ls1641175wmf.5.canary-gmail; Tue, 26
 Sep 2017 01:54:19 -0700 (PDT)
X-Received: by 10.223.164.71 with SMTP id e7mr8187320wra.4.1506416059112;
        Tue, 26 Sep 2017 01:54:19 -0700 (PDT)
ARC-Seal: i=1; a=rsa-sha256; t=1506416059; cv=none;
        d=google.com; s=arc-20160816;
        b=kAgH6uP6OLeuDk1u1ITj7mu9iQf2rhVq5dMD/rVar3+x03YDi19pM6wBbwM8516+pN
         EkYDD4Kx2tyyRoXToRMhtj8pS2VyK3GCTsnuK4Uq2OfbjhyMwFLJjNJHKwRfVU/m4It5
         KqqgdhKfuKbIpeV2aYZwzL35aU4VzNXPdKn8/SrivxGbj8ogR/a5fzy2ImwQioFjCBSP
         SXrt7qScuZNl2MqLWTaynd1kFYXPCDHPJqfsE5gPRtBNNhxynTM6Wy/aibEfiWb5Qwjc
         ifKFuceGB/oTOHJJxJwpLHc7b1jhr/cdrQ9r4iO3AigeigJ1y0NUmU1hY5orB1WrpXRO
         1Z6w==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=to:subject:message-id:date:from:references:in-reply-to:mime-version
         :dkim-signature:arc-authentication-results;
        bh=P0F1E9PJo/bHP+EPhtzBYFN9SUlpxH0S5pp7r0PiyrY=;
        b=yUEd/iRgSfubquLr833zpwol30dT5/I+t0lJ3Mp8X+TZp9GRdRTWl1Q/jQ7Tvu/Xm7
         S7wQKSujmDWkZlduAVY0fMzz7uiyEgc+aFN87lVmGuzoBoGMhbpnNARUkdHANnbiqSbu
         GdQcH9J/bfGH3u4fZMcl22vwjsyEiNItQUEkd65mfXmB55WwnDzCEJQBiWEOp6c4kx7f
         uVMowZEhXnPKAhSAyiPRPdeYyhKxEvt+zSKRlNiARLFP/g4Fljs3k1qkY/m9KGoVRUuy
         f/b68/UsFdSLgxu0ShpgjbJiNE1UDvyh0ZbNzrMQPa+fFJnZPtAD8nBbGklSfxnAEdtB
         cLYA==
ARC-Authentication-Results: i=1; mx.google.com;
       dkim=pass header.i=@metafoo.co.uk header.s=default header.b=YPaUH7CV;
       spf=neutral (google.com: 81.27.85.19 is neither permitted nor denied by best guess record for domain of richard@metafoo.co.uk) smtp.mailfrom=richard@metafoo.co.uk
Original-Received: from uk12.easy-internet.co.uk (uk12.easy-internet.co.uk. [81.27.85.19])
        by mx.google.com with ESMTPS id p3si6394720wrd.547.2017.09.26.01.54.18
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Tue, 26 Sep 2017 01:54:18 -0700 (PDT)
Received-SPF: neutral (google.com: 81.27.85.19 is neither permitted nor denied by best guess record for domain of richard@metafoo.co.uk) client-ip=81.27.85.19;
Original-Received: from mail-oi0-f51.google.com ([209.85.218.51]:56135)
	by uk12.easy-internet.co.uk with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128)
	(Exim 4.89)
	(envelope-from <richard@metafoo.co.uk>)
	id 1dwldB-0000jq-M9
	for std-proposals@isocpp.org; Tue, 26 Sep 2017 09:54:17 +0100
Original-Received: by mail-oi0-f51.google.com with SMTP id x85so10881096oix.12
        for <std-proposals@isocpp.org>; Tue, 26 Sep 2017 01:54:18 -0700 (PDT)
X-Received: by 10.202.252.215 with SMTP id a206mr12646549oii.85.1506416057743;
 Tue, 26 Sep 2017 01:54:17 -0700 (PDT)
Original-Received: by 10.74.91.133 with HTTP; Tue, 26 Sep 2017 01:53:57 -0700 (PDT)
In-Reply-To: <7bf0d2563477e89a8faa3ca2daa18f67@kdab.com>
X-Gmail-Original-Message-ID: <CAOfiQqnmBJ+H2a1R6pCeKkJqBYmOX3qK0ZFyNRMGngStSSChVA@mail.gmail.com>
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
X-AntiAbuse: Primary Hostname - uk12.easy-internet.co.uk
X-AntiAbuse: Original Domain - isocpp.org
X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse: Sender Address Domain - metafoo.co.uk
X-Get-Message-Sender-Via: uk12.easy-internet.co.uk: authenticated_id: metafooc/from_h
X-Authenticated-Sender: uk12.easy-internet.co.uk: richard@metafoo.co.uk
X-Source: 
X-Source-Args: 
X-Source-Dir: 
X-Original-Sender: richard@metafoo.co.uk
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@metafoo.co.uk header.s=default header.b=YPaUH7CV;       spf=neutral
 (google.com: 81.27.85.19 is neither permitted nor denied by best guess record
 for domain of richard@metafoo.co.uk) smtp.mailfrom=richard@metafoo.co.uk
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:34491
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/34491>

--001a113de226017dca055a13d248
Content-Type: text/plain; charset="UTF-8"

On 26 September 2017 at 00:53, Marc Mutz <marc.mutz@kdab.com> wrote:

> Hi,
>
> Why do these semantically identical pieces of code have different syntax
> rules?
>
>   template <typename Map, typename Lookup>
>   auto find(Map &map, const Lookup &key)
>   {
>       const auto it = map.find(key);
>       if (it == map.end())
>           return std::nullopt;
>       else
>           return std::optional{*it}; // error: inconsistent deduction for
> auto return type
>   }
>
>   template <typename Map, typename Lookup>
>   auto find(Map &map, const Lookup &key)
>   {
>       const auto it = map.find(key);
>       return it == map.end() ? std::nullopt : std::optional{*it}; // OK,
> -> std::optional<Map::value_type>
>   }
>
> iow: Why do auto return types have to have identical types, and don't just
> use the rules for the ternary operator?
>

Because some people thought it more important to allow self-recursive calls
after the first return statement:

auto f(int n) {
  if (n <= 1) return 1;
  return n * f(n - 1); // ok to call f here, return type already deduced
}

FWIW, I think we made the wrong tradeoff here, but this decision is
unlikely to be reconsidered now.

(We could use the ternary operator rules up until the first recursive use
of the function within itself, but that risks major confusion and surprises
when a recursive function is refactored and its return statements are
reordered...)


> And why, in the first case, can I not say
>
>    auto find(Map &map, const Lookup &key) -> std::optional
>    {
>
> iow: why doesn't ctor template argument deduction work for trailing return
> types?
>

An overabundance of conservatism. I expect this restriction will be relaxed
as soon as someone argues for it in committee.

Is/was this a conscious decision against, or would a proposal
> adding/changing these be welcome?
>
> Thanks,
> Marc
>
> --
> 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/is
> ocpp.org/d/msgid/std-proposals/7bf0d2563477e89a8faa3ca2daa18f67%40kdab.com
> .
>

-- 
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/CAOfiQqnmBJ%2BH2a1R6pCeKkJqBYmOX3qK0ZFyNRMGngStSSChVA%40mail.gmail.com.

--001a113de226017dca055a13d248
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 2=
6 September 2017 at 00:53, Marc Mutz <span dir=3D"ltr">&lt;<a href=3D"mailt=
o:marc.mutz@kdab.com" target=3D"_blank">marc.mutz@kdab.com</a>&gt;</span> w=
rote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;borde=
r-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
Why do these semantically identical pieces of code have different syntax ru=
les?<br>
<br>
=C2=A0 template &lt;typename Map, typename Lookup&gt;<br>
=C2=A0 auto find(Map &amp;map, const Lookup &amp;key)<br>
=C2=A0 {<br>
=C2=A0 =C2=A0 =C2=A0 const auto it =3D map.find(key);<br>
=C2=A0 =C2=A0 =C2=A0 if (it =3D=3D map.end())<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return std::nullopt;<br>
=C2=A0 =C2=A0 =C2=A0 else<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return std::optional{*it}; // error: inc=
onsistent deduction for auto return type<br>
=C2=A0 }<br>
<br>
=C2=A0 template &lt;typename Map, typename Lookup&gt;<br>
=C2=A0 auto find(Map &amp;map, const Lookup &amp;key)<br>
=C2=A0 {<br>
=C2=A0 =C2=A0 =C2=A0 const auto it =3D map.find(key);<br>
=C2=A0 =C2=A0 =C2=A0 return it =3D=3D map.end() ? std::nullopt : std::optio=
nal{*it}; // OK, -&gt; std::optional&lt;Map::value_type&gt;<br>
=C2=A0 }<br>
<br>
iow: Why do auto return types have to have identical types, and don&#39;t j=
ust use the rules for the ternary operator?<br></blockquote><div><br></div>=
<div>Because some people thought it more important to allow self-recursive =
calls after the first return statement:</div><div><br></div><div>auto f(int=
 n) {</div><div>=C2=A0 if (n &lt;=3D 1) return 1;</div><div>=C2=A0 return n=
 * f(n - 1); // ok to call f here, return type already deduced</div><div>}<=
/div><div><br></div><div>FWIW, I think we made the wrong tradeoff here, but=
 this decision is unlikely to be reconsidered now.</div><div><br></div><div=
>(We could use the ternary operator rules up until the first recursive use =
of the function within itself, but that risks major confusion and surprises=
 when a recursive function is refactored and its return statements are reor=
dered...)</div><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"=
margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
And why, in the first case, can I not say<br>
<br>
=C2=A0 =C2=A0auto find(Map &amp;map, const Lookup &amp;key) -&gt; std::opti=
onal<br>
=C2=A0 =C2=A0{<br>
<br>
iow: why doesn&#39;t ctor template argument deduction work for trailing ret=
urn types?<br></blockquote><div><br></div><div>An overabundance of conserva=
tism. I expect this restriction will be relaxed as soon as someone argues f=
or it in committee.</div><div><br></div><blockquote class=3D"gmail_quote" s=
tyle=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Is/was this a conscious decision against, or would a proposal adding/changi=
ng these be welcome?<br>
<br>
Thanks,<br>
Marc<span class=3D"HOEnZb"><font color=3D"#888888"><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%2Bunsubscribe@isocpp.org" target=3D=
"_blank">std-proposals+unsubscribe@isoc<wbr>pp.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/7bf0d2563477e89a8faa3ca2daa18f67%40kd=
ab.com" rel=3D"noreferrer" target=3D"_blank">https://groups.google.com/a/is=
<wbr>ocpp.org/d/msgid/std-proposals<wbr>/7bf0d2563477e89a8faa3ca2daa18<wbr>=
f67%40kdab.com</a>.<br>
</font></span></blockquote></div><br></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/CAOfiQqnmBJ%2BH2a1R6pCeKkJqBYmOX3qK0Z=
FyNRMGngStSSChVA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAOfiQqnmBJ%2BH=
2a1R6pCeKkJqBYmOX3qK0ZFyNRMGngStSSChVA%40mail.gmail.com</a>.<br />

--001a113de226017dca055a13d248--

.
