220 30174 <1daf67ad-8ee7-4ad6-937b-027ba6020c14@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, 3 Jan 2017 07:21:36 -0800 (PST)
Lines: 77
Approved: news@gmane.org
Message-ID: <1daf67ad-8ee7-4ad6-937b-027ba6020c14@isocpp.org>
References: <efdf2357-00a6-408a-8772-12f99135a880@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_1545_1639964131.1483456896854"
X-Trace: blaine.gmane.org 1483456908 5240 195.159.176.226 (3 Jan 2017 15:21:48 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Tue, 3 Jan 2017 15:21:48 +0000 (UTC)
Cc: m.cencora@gmail.com
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCEKFTV6ZUMBBAMDV7BQKGQEG3OCIOY@isocpp.org Tue Jan 03 16:21:44 2017
Return-path: <std-proposals+bncBCEKFTV6ZUMBBAMDV7BQKGQEG3OCIOY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pg0-f70.google.com ([74.125.83.70])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCEKFTV6ZUMBBAMDV7BQKGQEG3OCIOY@isocpp.org>)
	id 1cOQu6-0008Cy-Bz
	for gclcip-std-proposals@m.gmane.org; Tue, 03 Jan 2017 16:21:34 +0100
Original-Received: by mail-pg0-f70.google.com with SMTP id a190sf1238952696pgc.0
        for <gclcip-std-proposals@m.gmane.org>; Tue, 03 Jan 2017 07:21:38 -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=K8zBtOXS/8xRLEjhunPxd+iyN20HmnU1aysixHMz7Pg=;
        b=z8cpQsd+CUmuTtLF+PgO4Eurf5vuKUUlSm6SPjFx7JRCYqaQvvMP7y3SfUloe82dgP
         diSckNbU1NwAz/EuZRnk9rK8bW6lU/hWEnPeJOMfXqLcpE6iIs4ld8sf/9/whGcgWPvL
         oE0QrqbYaUOLIOJQPGlDtHhQ7GVDJdGkJzSSkP7RGHbpjh67rOlSJBnGCOJaR+NryDho
         x5s1QdeuuLTxgaDgJHo9NhTovUqd9chnWzcRFaksmgCvEBIVoGLDUXha2AMr20DRyouG
         JuCfJT6PDxaAwpDVWfThtvhgPqqqcvSJRxxibpH3XFR05RFJEPMkofkHQqaDcVQTar+y
         1Zaw==
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=K8zBtOXS/8xRLEjhunPxd+iyN20HmnU1aysixHMz7Pg=;
        b=B9b5KOeBsLqmDh6Y739OOM8I3m27nplVjUmEmNd68pFzqauDp0+U2gs2TgvntTTaOH
         Qx7coSULoCgrGkwstG2xHu3i7yfZUj8W7GrLSsdtKpNHraBdh1NCvlcyk/tBowzS9/g2
         E2DBi4OWxlEAtVIUcm529NVgF52dJvkOhhfJn2ejz0TxSUS/ZMBqpyEcuNJQ62bNvarR
         xB1vlBCUfsup8E+uChgCok/JszaZp1iyjM0WhcHsYF7YEK/r0gIXPNlRUmyjdfLC7wWa
         lGmO2KAemQc0pfTO0ELAilHP5b0G/yH66ZGEhPaUZE5kXFvgAwvp/38RGA1vke0x0fU4
         4+CQ==
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=K8zBtOXS/8xRLEjhunPxd+iyN20HmnU1aysixHMz7Pg=;
        b=HfPJ5zwBehO7/8Q4YwiZaDXektTppHxAzwr/jdFgermR64dy6gm42V79ueTCaPU0q2
         79HmzeTRDhfDnaT+voCaVND79UqSlcK1yXzh7GAsv3L4tAm366APV+gLGYgPY+4ncbLO
         4HSYcpZSwxvJggtGIdaIdw6l1KcSsjheEuGC1hBTqIdfKMleNDbXXNkwTpSlfepgy8ni
         vJBaSy8gYDnRSoOGlEpz1qSIx99kLCTMTan+zE8tDfD1sV4a1AmfoANstiTkWsYFA62o
         2iqLq0bx+ym9Ky5mdTQZpJp6dkPQAKl7xoM0TyEnhyxUYJaHbEMhYiEMw4OsVsmUZkGu
         SsOg==
X-Gm-Message-State: AIkVDXLX367r3b/rB/ytXhB2fQ71Cebtu787lCaSAXc2NXp3/wQ1IH057WxhEN9mwS6rmw==
X-Received: by 10.99.234.5 with SMTP id c5mr36793210pgi.150.1483456898122;
        Tue, 03 Jan 2017 07:21:38 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.157.32.170 with SMTP id x39ls40704937ota.23.gmail; Tue, 03 Jan
 2017 07:21:37 -0800 (PST)
X-Received: by 10.157.20.197 with SMTP id r5mr2851149otr.9.1483456897422;
        Tue, 03 Jan 2017 07:21:37 -0800 (PST)
In-Reply-To: <efdf2357-00a6-408a-8772-12f99135a880@isocpp.org>
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:30174
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/30174>

------=_Part_1545_1639964131.1483456896854
Content-Type: multipart/alternative; 
	boundary="----=_Part_1546_1710949020.1483456896854"

------=_Part_1546_1710949020.1483456896854
Content-Type: text/plain; charset=UTF-8

On Tuesday, January 3, 2017 at 4:56:05 AM UTC-5, m.ce...@gmail.com wrote:
>
> Hi,
>
> I propose to add a enum_cast function that safely casts int value to a 
> target enum type if int value represents a valid enumerator.
>

It should not return an `optional`. It should either correctly cast or 
*throw*. Why?

Consider how users will use it. You're going to see a lot of people doing 
`enum_cast<>().value()`. Which will incur needless copying if the 
conversion worked. And if it doesn't work, an exception will be thrown.

What exception will that be? `bad_optional_access`. Which is about as 
useless as exceptions get as far as tracking down the problem. A far more 
descriptive exception would be `bad_enum_cast`.

I would say that you should provide both APIs: `enum_cast` for conversion 
or throwing. And `optional_enum_cast` or somesuch for conversion into an 
`optional<enum>`.

-- 
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/1daf67ad-8ee7-4ad6-937b-027ba6020c14%40isocpp.org.

------=_Part_1546_1710949020.1483456896854
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Tuesday, January 3, 2017 at 4:56:05 AM UTC-5, m.ce...@g=
mail.com wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-=
left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr=
">Hi,<div><br></div><div>I propose to add a enum_cast function that safely =
casts int value to a target enum type if int value represents a valid enume=
rator.</div></div></blockquote><div><br>It should not return an `optional`.=
 It should either correctly cast or <i>throw</i>. Why?<br><br>Consider how =
users will use it. You&#39;re going to see a lot of people doing `enum_cast=
&lt;&gt;().value()`. Which will incur needless copying if the conversion wo=
rked. And if it doesn&#39;t work, an exception will be thrown.<br><br>What =
exception will that be? `bad_optional_access`. Which is about as useless as=
 exceptions get as far as tracking down the problem. A far more descriptive=
 exception would be `bad_enum_cast`.<br><br>I would say that you should pro=
vide both APIs: `enum_cast` for conversion or throwing. And `optional_enum_=
cast` or somesuch for conversion into an `optional&lt;enum&gt;`.</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/1daf67ad-8ee7-4ad6-937b-027ba6020c14%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/1daf67ad-8ee7-4ad6-937b-027ba6020c14=
%40isocpp.org</a>.<br />

------=_Part_1546_1710949020.1483456896854--

------=_Part_1545_1639964131.1483456896854--

.
