220 34496 <CABsSThqzT3jY3hqUATysuMqz_hY5kB4Jmg5Z2izUn1VWqVKzvQ@mail.gmail.com> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Faisal Vali <faisalv@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Auto return type deduction surprises
Date: Tue, 26 Sep 2017 15:24:29 -0500
Lines: 102
Approved: news@gmane.org
Message-ID: <CABsSThqzT3jY3hqUATysuMqz_hY5kB4Jmg5Z2izUn1VWqVKzvQ@mail.gmail.com>
References: <7bf0d2563477e89a8faa3ca2daa18f67@kdab.com> <CAOfiQqnmBJ+H2a1R6pCeKkJqBYmOX3qK0ZFyNRMGngStSSChVA@mail.gmail.com>
 <25067783-13f0-4943-9736-f7326f1567d7@isocpp.org> <072abf8e-251c-4fe5-a24f-80c9319e3326@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: blaine.gmane.org 1506457502 13908 195.159.176.226 (26 Sep 2017 20:25:02 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Tue, 26 Sep 2017 20:25:02 +0000 (UTC)
To: "<std-proposals@isocpp.org>" <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBDDKRIXX5YARBHHPVLHAKGQERKQEP5Y@isocpp.org Tue Sep 26 22:24:57 2017
Return-path: <std-proposals+bncBDDKRIXX5YARBHHPVLHAKGQERKQEP5Y@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-oi0-f70.google.com ([209.85.218.70])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDDKRIXX5YARBHHPVLHAKGQERKQEP5Y@isocpp.org>)
	id 1dwwPW-0003Ei-KU
	for gclcip-std-proposals@m.gmane.org; Tue, 26 Sep 2017 22:24:54 +0200
Original-Received: by mail-oi0-f70.google.com with SMTP id a74sf6132175oib.10
        for <gclcip-std-proposals@m.gmane.org>; Tue, 26 Sep 2017 13:25:02 -0700 (PDT)
ARC-Seal: i=2; a=rsa-sha256; t=1506457501; cv=pass;
        d=google.com; s=arc-20160816;
        b=Nf2KuAbL0rqwHYBnLer1woAjFItprhx9doYgZiXJokKpzxsqiUYEr5vfaDa8r4ZBZo
         mcmkd+45RFpZKfH7yaIZXrI4JPFkrJxaggQZ1wYOgdqJDd8cTCtAAA7w1L/sDJlctKuS
         9E+/izQ8Sg13SS2B84BT7teozMsdyovYotd5rAz+Bd/bDv+7R2oQYePcSHZG8E3pzpew
         upX+5bMTl5oRJcQDh9QTGF1FUlITwaP/uYqsV9yUgO+LmE24IDl/v2cY1zzs/9fqkv0j
         aG9/WH9ninFvZL+lGhY6vQivIgW3H1BNizwektzo6TGYz21Q3w0CZLiSwJZx1pSqzVgA
         pQzg==
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:to:subject:message-id:date:from
         :references:in-reply-to:reply-to:mime-version
         :arc-authentication-results:arc-message-signature:dkim-signature
         :arc-authentication-results;
        bh=qpulpDlLf0NkIptZEkUO9UezkjW0Nx+72YMsgedqyLU=;
        b=NSpVvXKeuKx7OMLa6CuY9Y8Dhbz1ktWIxXkbOnwD9AFGHhLAZA5NPa0e2zgPsBRU3Z
         zQn/vwuKiwDfYiUPGSAMVsgztoshy11kUMDyL+AtwH3xC6isF8A9z/EuQJZTLQGklPOV
         SuSPn3oXH8+rsHOdipAZXfUHD+A1HKIoxvQGkNXH2ifg/+n9gIft020bhbUvFuwGJmzY
         dg+B7TgM5tFGgmw7FIseIlcMiyLvJ/66KkBgpWzTCeobrct+zzzNQy1AKwtQFmGQp8+1
         XdFVi2XHI63uII5G9VRLUYzcIVtx6G4xBCSxD8qp3T95arAjH2rkywhx4hkD74svnU9P
         mmdw==
ARC-Authentication-Results: i=2; mx.google.com;
       dkim=pass header.i=@gmail.com header.s=20161025 header.b=XIcDielJ;
       spf=pass (google.com: domain of faisalv@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=faisalv@gmail.com;
       dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=mime-version:reply-to:in-reply-to:references:from:date:message-id
         :subject:to:x-original-sender:x-original-authentication-results
         :precedence:mailing-list:list-id:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=qpulpDlLf0NkIptZEkUO9UezkjW0Nx+72YMsgedqyLU=;
        b=ipz2TLWiG0T/NhVuXkTZEc5S1CHfStehwG3JBGxfoV3JFPVALYr6p7GrMBvSc8q5Jq
         cJTQ+V8dZ7oiC5AJkBOeD5XZthm3k8yDPSPWRYdBj73kuJcCmTQHIhkE87bWSGGuqJ4r
         GLBY5sa1EIxnVNR8TxxZAR3Fuq3my3Goul1Fyi9VkbYdygdy1erHeaq2/PSEN7rqBjEm
         Qjc/iI9LwhAYip6I8gwnr42L3jxh3xWY6sSQ2A6lX2Otsaz8eYM79AbbDaEIwWXDzK4X
         xXU38pURMa5lSstZ7TiUWiAIW3X80hY6BvLRiC4PJcpHp3k8TiOX81lOp6Qy/USiFbZY
         Tagw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:mime-version:reply-to:in-reply-to:references
         :from:date:message-id:subject:to:x-original-sender
         :x-original-authentication-results:precedence:mailing-list:list-id
         :x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=qpulpDlLf0NkIptZEkUO9UezkjW0Nx+72YMsgedqyLU=;
        b=d2dZHK1nbajNZ95LG7YMGt/xp7RJxxu82xNzb9IRxW6KWvPb9zux/rUs5n0rwFGeWp
         LjHWQWp3sJJSDiFDRYuqaAVNguXNE6PF5wsDoLJU4hSC8WEmbGRM3+PjmNNmu64SA7Lx
         UGd7/qzdmnh/RoF3hd6Ls9Cxr/Ui0n6sztEt3a7NtpnFi05zqGp4UOP1pd4gZCqDFHqk
         R8XKaaCWWBuIR9qmeMpK9wEDSlBpfBqFdug2kIuz+gIDBlEivtoJKAcpMA34hmwHrv93
         Hvu6SKDjUMqSxOe/3J/INvLhsOP5hbWf5Wp5SMJrhXm/eMIaDGlvdBsmkuFcwJNtDDVD
         uQRQ==
X-Gm-Message-State: AHPjjUi2GohydT/T6b7AOjk2Sb2VUY+2qDwELx4TwIZ8ldVB/5Dvt1Pg
	PTVvz/4A6SKauH3s9fM7cSLt9g==
X-Google-Smtp-Source: AOwi7QBk8D4kTEQeAw2O0s5kj8avSP2ZPQeuqPYOAblK+M/J1LCGxOw1bfu1qz/9T/uD9JArgDFJZA==
X-Received: by 10.157.39.162 with SMTP id c31mr1899980otb.18.1506457501841;
        Tue, 26 Sep 2017 13:25:01 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.202.97.70 with SMTP id v67ls6560568oib.11.gmail; Tue, 26 Sep
 2017 13:25:00 -0700 (PDT)
X-Received: by 10.202.237.81 with SMTP id l78mr1133968oih.356.1506457500594;
        Tue, 26 Sep 2017 13:25:00 -0700 (PDT)
ARC-Seal: i=1; a=rsa-sha256; t=1506457500; cv=none;
        d=google.com; s=arc-20160816;
        b=iMKzLy2U6v9bWLIXU74oF1IYa19lD/1jxeY0EdqgMI83/0WcfOvG28M0VaKmOfMvVI
         YzFvcbAheRbPqDtw2un2SWXvwlUU6pz8q9d6g/NiGYfFkEcNKZQQ6aMjPIkRlWLYEdfL
         bop1YhmQklxjnt1sEdfkH9+N5QRxG01D2TPZ5Zvlf9fPtv6gfSc5B+WZ7a2FCL/kGKMs
         MVBqoHzw5sfF6gARML99dQ0fqOlq3jlWWOjQeJR9oD4zXxSGBZv0q4HnhM/QD3+yfLg5
         m1JW766ejrzWb1mii8qrJYLNSNl0u3ykywPTPmkJkSOBx+uYdIIaTPhgI/r2uFkDk6Sa
         LRxg==
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:reply-to
         :mime-version:dkim-signature:arc-authentication-results;
        bh=OOEl13MQyP1y8dGpG+lqGVAbg8x07KE/69KL//1NvE4=;
        b=pBmKHCn0GPI/6Gdaue71djrXrSibBghAjFou1+FYNSUAbbXeo6Boli4dmGum846JAg
         H4iawZtBBS5E+KcHpN4LUkZZs01vDy+SYB9KU0y0Ty9F0Cktz+HnMofg4AAbCHQs5CsI
         FlRNROAFV/gLQoTF5WEtIQxpQDE3ebstnydPdprZmvlWYu1t3UG4JfPJzY10BDEpawal
         71MfnfPAfaR+ljVVwT2GJ1I0BxirwyEiTDGARiEBfKMCu6bTfGEwNrJspmBpvMnAxmhz
         7QcDPtFK3BgKFVQNxdyX8wYO3LWfFv/owkbzEw0MraQc1x0jltjychPRLwzqa/cdfkHH
         M0Yw==
ARC-Authentication-Results: i=1; mx.google.com;
       dkim=pass header.i=@gmail.com header.s=20161025 header.b=XIcDielJ;
       spf=pass (google.com: domain of faisalv@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=faisalv@gmail.com;
       dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com
Original-Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41])
        by mx.google.com with SMTPS id x11sor4132446oix.305.2017.09.26.13.25.00
        for <std-proposals@isocpp.org>
        (Google Transport Security);
        Tue, 26 Sep 2017 13:25:00 -0700 (PDT)
Received-SPF: pass (google.com: domain of faisalv@gmail.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41;
X-Received: by 10.202.239.138 with SMTP id n132mr11962112oih.301.1506457500001;
 Tue, 26 Sep 2017 13:25:00 -0700 (PDT)
Original-Received: by 10.157.22.167 with HTTP; Tue, 26 Sep 2017 13:24:29 -0700 (PDT)
In-Reply-To: <072abf8e-251c-4fe5-a24f-80c9319e3326@isocpp.org>
X-Original-Sender: faisalv@gmail.com
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@gmail.com header.s=20161025 header.b=XIcDielJ;       spf=pass
 (google.com: domain of faisalv@gmail.com designates 209.85.220.41 as
 permitted sender) smtp.mailfrom=faisalv@gmail.com;       dmarc=pass (p=NONE
 sp=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:34496
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/34496>

On Tue, Sep 26, 2017 at 2:04 PM, T. C. <rs2740@gmail.com> wrote:
> On Tuesday, September 26, 2017 at 11:21:35 AM UTC-4, Nicol Bolas wrote:
>>
>> On Tuesday, September 26, 2017 at 4:54:21 AM UTC-4, Richard Smith wrote:
>>>
>>> On 26 September 2017 at 00:53, Marc Mutz <marc...@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.
>>
>>
>> Personally, I think it's a good move. Not for self-recursion reasons but
>> for sanity and error checking reasons.
>>
>> With the ?: operator, the two expressions are right next to each other.
>> This means that if you can see one expression, you can see the other. As
>> such, you can easily see from that one line what the overall expression
>> resolves to. It requires more mental thought, but all of the information is
>> right there in the same statement.
>>
>> Return statements can be pages apart, depending on function length. As
>> such, it's much more difficult to know what `std::common_type` will resolve
>> to; figuring that out requires bouncing between all of the return statements
>> in the function. So in order to know what the function's return type is, you
>> have to read the entire thing.
>
>
> Also, we won't be able to use common_type's semantics, which is order
> dependent for >2 arguments. That would be insane for this case.
>
> We'd have to generalize ?:'s semantics directly for >2 operands if we want
> to go down that path.
>

Years ago, when i was interested in this, I had come up with something
similar - my unsophisticated implementation (if i remember correctly)
was O(N^2) in the number of return statements (but i think i felt this
could be optimized...).

Implementation:
https://github.com/faisalv/clang/commits/cxx1z-conditional-return-type-deduction

Test examples: https://github.com/faisalv/clang/blob/cxx1z-conditional-return-type-deduction/test/SemaCXX/cxx1z-deduced-return-type.cpp

I also had some thoughts on how to merge return statements, returning
different lambda expressions/or closure objects...

But I think I agree with Richard, unless some very strong forces raise
some very loud resonant noises, this ship might have sailed for
good...

> --
> 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/072abf8e-251c-4fe5-a24f-80c9319e3326%40isocpp.org.

-- 
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/CABsSThqzT3jY3hqUATysuMqz_hY5kB4Jmg5Z2izUn1VWqVKzvQ%40mail.gmail.com.

.
