220 30469 <d974c090-2898-4397-8d42-bbc1682158c7@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Nicol Bolas <jmckesson@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: enum_cast proposal
Date: Tue, 10 Jan 2017 11:09:42 -0800 (PST)
Lines: 139
Approved: news@gmane.org
Message-ID: <d974c090-2898-4397-8d42-bbc1682158c7@isocpp.org>
References: <efdf2357-00a6-408a-8772-12f99135a880@isocpp.org>
 <57258435-5d82-48e7-8ed7-3c38682418fb@isocpp.org>
 <a35d9f20-9e4f-4789-b7e5-5cb08f99e6bd@isocpp.org>
 <58750288.5080004@gmail.com>
 <e85f0d46-f244-42fe-a276-b4e18b3d627f@isocpp.org>
 <587512E1.1050708@gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_1667_1998639733.1484075382524"
X-Trace: blaine.gmane.org 1484075391 20143 195.159.176.226 (10 Jan 2017 19:09:51 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Tue, 10 Jan 2017 19:09:51 +0000 (UTC)
Cc: mwoehlke.floss@gmail.com
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCEKFTV6ZUMBB57C2TBQKGQEFS3JC7Y@isocpp.org Tue Jan 10 20:09:45 2017
Return-path: <std-proposals+bncBCEKFTV6ZUMBB57C2TBQKGQEFS3JC7Y@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pf0-f197.google.com ([209.85.192.197])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCEKFTV6ZUMBB57C2TBQKGQEFS3JC7Y@isocpp.org>)
	id 1cR1nf-00040K-RQ
	for gclcip-std-proposals@m.gmane.org; Tue, 10 Jan 2017 20:09:40 +0100
Original-Received: by mail-pf0-f197.google.com with SMTP id z128sf300288254pfb.4
        for <gclcip-std-proposals@m.gmane.org>; Tue, 10 Jan 2017 11:09:44 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:cc:message-id:in-reply-to:references: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=Lg7ZbAaT/+fBxV/owIG3WErgNriIbmfx3COHdYMQ0yc=;
        b=UpUjrOss6nrTJJWMC0lIqg3doJ03hzHEAHcGmNwp6dRB+S0kXsHiGJiZPgAyAhwwU4
         cVQBNB/Mv6KbhEBmYgoBB5k78NfVVupJdBUt4tPXm95XdGxjcepOkGslzilqvOZtGyFb
         wYI1cD/KCqS5CtPcuZeRHL6LTWe7MlNCQJFncga3+MOLlidQ+jcKjHRF14OmYUvPrT6w
         9gez8kTkgZH59K/Fc61yDQrV0Uuh0Vio7t+sm/56aNxUOD2AGdUznJszPhFimkgSsxd8
         sjvvhTxD+NG0ypXY6DKPxGOsPNDOtHxSDk4/OWb+ps7/j3orNLpFjcdInWLwzcKbDQt/
         cmzQ==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=date:from:to:cc:message-id:in-reply-to:references: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=Lg7ZbAaT/+fBxV/owIG3WErgNriIbmfx3COHdYMQ0yc=;
        b=vAbI8zHnwg17qu90SE/q6qDtk9rtCqtqmj+EJd+h68S88JmG8JnU1O8HYHxgLsoJo/
         f/0E3bdc8KgFiWBGkaEQf2Km9DR7Ch1Nxb1nwLbCGTcw5r2DsxLB1lmcMiCuE9uKjzz9
         lEjlnwapRuf/JPVeBVFQ9uUultWwPqHHF2hOL1kmBtIcDp7/GifAP7DALhmMI3es9Dvu
         efLtErt5JpFk+Pn/0Q+aHq7JnVhN+oBdXJ+qTyicmwIXERUs3XEhVoeqCzVrUtW09726
         EYIaazsSg2R/v5bTUSfQA8AtrCcfwYxd0+OHPnwe7Xz8ayLLZePCuLU1wjFs2GkBwcCL
         9Djg==
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:cc:message-id:in-reply-to
         :references: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=Lg7ZbAaT/+fBxV/owIG3WErgNriIbmfx3COHdYMQ0yc=;
        b=n/3iSx5mEq8zSriD5mP2NtPeaIfXHAgQkZ6pIWvZXBNcuDX2/rF3IFibI5fTUbyJGZ
         ujnvDsNwE//fSWtHz+HI3eKl1rYT9pGOp4tD+omtnpSrdzyRj17NFXl69nhamqdBDlch
         +x9wSjODuR804gFImuTcQs8bJGqJxph/NiMy436xSNC2feq1TVJRxua4BuicBmS/gTAR
         2MbJmn0wkN2TOupNtDDkOfQbkqXHjDeKRO5DblTXF7nDYtza/ORSrXXvQVn0+e01zTJD
         MS6TKcq/QaGrf6s+P5i9L60h0ZEM1zHAwoW9zdQRSHkKV2KIFGr9X8AKX45mesbznfkW
         oUpw==
X-Gm-Message-State: AIkVDXKPpNl4XYD2uPSwNBJCLLOv1R6qO7Rx37MWSVdGJyBMXGxkYy9wprB8mZSc/rbSZw==
X-Received: by 10.99.102.68 with SMTP id a65mr1641829pgc.122.1484075383764;
        Tue, 10 Jan 2017 11:09:43 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.157.17.154 with SMTP id v26ls2311047otf.0.gmail; Tue, 10 Jan
 2017 11:09:43 -0800 (PST)
X-Received: by 10.157.18.211 with SMTP id g77mr321738otg.14.1484075383037;
        Tue, 10 Jan 2017 11:09:43 -0800 (PST)
In-Reply-To: <587512E1.1050708@gmail.com>
X-Original-Sender: jmckesson@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:30469
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/30469>

------=_Part_1667_1998639733.1484075382524
Content-Type: multipart/alternative; 
	boundary="----=_Part_1668_1585808523.1484075382525"

------=_Part_1668_1585808523.1484075382525
Content-Type: text/plain; charset=UTF-8

On Tuesday, January 10, 2017 at 11:59:16 AM UTC-5, Matthew Woehlke wrote:
>
> On 2017-01-10 11:20, Nicol Bolas wrote: 
> > On Tuesday, January 10, 2017 at 10:49:34 AM UTC-5, Matthew Woehlke 
> wrote: 
> >> On 2017-01-10 10:31, m.ce...@gmail.com <javascript:> wrote: 
> >>> For sure baseline API must be exception-free. 
> >>> Do we really want to additionally support API that throws on error? 
> >> 
> >> I *really* hope we can use something like std::expected; this is a 
> >> textbook use case for that, and makes it trivial to have a 
> >> throw-on-error API (just grab the value from the expected without 
> >> checking it first; this will turn around and throw if the value is not 
> >> there). 
> > 
> > But the fact that it's throwing the wrong exception makes it useless 
> from 
> > an exception handling perspective. 
>
> Huh? I'm talking about std::expected, not std::optional. If you didn't 
> get a value, you get whatever error/exception (preferably an exception 
> in this case, since it doesn't need more storage than the enum and thus 
> doesn't cost extra) the API stuffed in instead, which would presumably 
> be a std::range_error or std::bad_enum_cast or whatever. IOW, the *SAME* 
> exception that an API that throws right away would throw.
>

Actually, you can't do that. Well, not in P0323 `expected`. It has a 
specific requirement that `E` is no-throw move constructible (in order to 
ensure never-empty without bloat). But `std::range_error` has no such 
guarantee on its move constructor. Also, it throws 
`bad_expected_access<E>`, not `E` itself.

Even ignoring that, you want the API to return `std::expected<Enum, 
std::range_error>`. Well, `std::range_error` derives from 
`std::runtime_error`. And that internally stores a string. Not necessarily 
`std::string`, but it stores a sequence of characters it dynamically 
allocates, along with the means to delete it. And whatever that storage may 
be, it's almost certainly bigger than `sizeof(Enum)`.

Which now means that your return type is needlessly bloated. If you had 
returned `std::optional<Enum>`, there would be no bloat.

Generally speaking, you should never use the *actual exception type* that 
you would have otherwise thrown as the non-expected value in an `expected`. 
`expected` errors are intended to be *lightweight*: error codes and the 
like. Not things that own memory and so forth. By contrast, exceptions can 
be quite heavy.

-- 
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/d974c090-2898-4397-8d42-bbc1682158c7%40isocpp.org.

------=_Part_1668_1585808523.1484075382525
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Tuesday, January 10, 2017 at 11:59:16 AM UTC-5, Matthew=
 Woehlke wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-=
left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On 2017-01-10 1=
1:20, Nicol Bolas wrote:
<br>&gt; On Tuesday, January 10, 2017 at 10:49:34 AM UTC-5, Matthew Woehlke=
 wrote:
<br>&gt;&gt; On 2017-01-10 10:31, <a>m.ce...@gmail.com</a> &lt;javascript:&=
gt; wrote:=20
<br>&gt;&gt;&gt; For sure baseline API must be exception-free.=20
<br>&gt;&gt;&gt; Do we really want to additionally support API that throws =
on error?=20
<br>&gt;&gt;
<br>&gt;&gt; I *really* hope we can use something like std::expected; this =
is a=20
<br>&gt;&gt; textbook use case for that, and makes it trivial to have a=20
<br>&gt;&gt; throw-on-error API (just grab the value from the expected with=
out=20
<br>&gt;&gt; checking it first; this will turn around and throw if the valu=
e is not=20
<br>&gt;&gt; there).
<br>&gt;=20
<br>&gt; But the fact that it&#39;s throwing the wrong exception makes it u=
seless from=20
<br>&gt; an exception handling perspective.
<br>
<br>Huh? I&#39;m talking about std::expected, not std::optional. If you did=
n&#39;t
<br>get a value, you get whatever error/exception (preferably an exception
<br>in this case, since it doesn&#39;t need more storage than the enum and =
thus
<br>doesn&#39;t cost extra) the API stuffed in instead, which would presuma=
bly
<br>be a std::range_error or std::bad_enum_cast or whatever. IOW, the *SAME=
*
<br>exception that an API that throws right away would throw.<br></blockquo=
te><div><br>Actually, you can&#39;t do that. Well, not in P0323 `expected`.=
 It has a specific requirement that `E` is no-throw move constructible (in =
order to ensure never-empty without bloat). But `std::range_error` has no s=
uch guarantee on its move constructor. Also, it throws `bad_expected_access=
&lt;E&gt;`, not `E` itself.<br><br>Even ignoring that, you want the API to =
return `std::expected&lt;Enum, std::range_error&gt;`. Well, `std::range_err=
or` derives from `std::runtime_error`. And that internally stores a string.=
 Not necessarily `std::string`, but it stores a sequence of characters it d=
ynamically allocates, along with the means to delete it. And whatever that =
storage may be, it&#39;s almost certainly bigger than `sizeof(Enum)`.<br><b=
r>Which now means that your return type is needlessly bloated. If you had r=
eturned `std::optional&lt;Enum&gt;`, there would be no bloat.<br><br>Genera=
lly speaking, you should never use the <i>actual exception type</i> that yo=
u would have otherwise thrown as the non-expected value in an `expected`. `=
expected` errors are intended to be <i>lightweight</i>: error codes and the=
 like. Not things that own memory and so forth. By contrast, exceptions can=
 be quite heavy.</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/d974c090-2898-4397-8d42-bbc1682158c7%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/d974c090-2898-4397-8d42-bbc1682158c7=
%40isocpp.org</a>.<br />

------=_Part_1668_1585808523.1484075382525--

------=_Part_1667_1998639733.1484075382524--

.
