220 30202 <e6c43713-ad62-4a5e-8eda-23c59bb67dd9@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: gmisocpp@gmail.com
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: enum_cast proposal
Date: Tue, 3 Jan 2017 15:01:15 -0800 (PST)
Lines: 187
Approved: news@gmane.org
Message-ID: <e6c43713-ad62-4a5e-8eda-23c59bb67dd9@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_5498_84607148.1483484476010"
X-Trace: blaine.gmane.org 1483484484 18012 195.159.176.226 (3 Jan 2017 23:01:24 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Tue, 3 Jan 2017 23:01:24 +0000 (UTC)
Cc: m.cencora@gmail.com
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCM3TRNUXUDBBPG2WDBQKGQE7HGPBEY@isocpp.org Wed Jan 04 00:01:20 2017
Return-path: <std-proposals+bncBCM3TRNUXUDBBPG2WDBQKGQE7HGPBEY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pf0-f200.google.com ([209.85.192.200])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCM3TRNUXUDBBPG2WDBQKGQE7HGPBEY@isocpp.org>)
	id 1cOY4v-0003Xo-I1
	for gclcip-std-proposals@m.gmane.org; Wed, 04 Jan 2017 00:01:13 +0100
Original-Received: by mail-pf0-f200.google.com with SMTP id c4sf756368627pfb.7
        for <gclcip-std-proposals@m.gmane.org>; Tue, 03 Jan 2017 15:01:18 -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=r2Qn3PKO12wPK1nEICjCZjFxOO1npkvyPWpe6K5DbnI=;
        b=pJrDlxhJPilR/y3oQCdF9k2+nWWx/BX1+9BPfRT4rFUJCSxFUt+jFaplZcPifSV/pF
         0SQGE3tO8/yDaI47tuA6b8GJDaz4UVzXPDWcbFzPjEjBNOxuhRv3ymUPUeel1QXnsiee
         kcss0Le8lERyu8W6Zts4rBeehrn9g8ngTFNyfRjwQdOmdQZcukjDDTsBvIS7kmFULlmh
         Akke4x4pIfiFsch516aMQxzUZf+YxOpXNYocaLJDnITpuQmBfkIRF2AH58lPGDtCnmpZ
         crbfOFG/pcTEDp9L/nVN4sD/pSLv841RAa/TWnX8jKUiEgW0hEVd7eXvW4XrD8yMQh+c
         hglg==
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=r2Qn3PKO12wPK1nEICjCZjFxOO1npkvyPWpe6K5DbnI=;
        b=a+iP4IxFPYTOmNbrIqWDhqxZg3yEIjkTjlTGN6iBFP4s5HUUNNo5heDpyWn5UwToqL
         mVFssmovMHftb4xJw3d76CvT1YsHuKxC3uNZUTXQcvGi+D2PQKaP7cNON1kr83Sq2nq5
         qJEDjQ6A9Xm1SP4PD4cDDqnxZ/lbfY3G3PsKXJUBeizSnkLrVFNl10a8hZousMmnVmpT
         +Ksef8Jencdl6yAiZG8QSbffIZ8ix+cAHA68htPwPb8wkBjhmTNzmkA/OGfZF45I/iZQ
         WUMDnK3NfLvawnJEPK51veEGIjEZO8+CePdLb+apPU5yffO/eAKwl0pWX/96id0MT1yK
         DXOw==
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=r2Qn3PKO12wPK1nEICjCZjFxOO1npkvyPWpe6K5DbnI=;
        b=ZrsCP+Mey5uJH/u7PfIOekh/v+MMviqAOd+O2WdrJqCP5Qt1vkLPUXwh2Fz0S3v0xZ
         TR2RCz/egyDPvm88gw+aVSElRE6djsQYtmIpy0pQtUxaASxYCx/yKUNojG7/Paxyuxsp
         SaL8eHqMyNNw2nhFFwQK3fVUVYS3DOIJSR3yMvE0hMgUFI5ykxC2EKo6MrP3UjyhL/JH
         jOi8XzSmNeOSU3m8A8L8o4nyFlqOrjRS4TjIyldA1uCd5m3Sei9C5CQe3nL7L9TMTfLC
         GxVXHbrxcv1wRRvTfF5WYCvrAzZZz44XcjAiyTON/IfIiNuvOpOv5kFhu7YyX+jFPx9T
         jk5A==
X-Gm-Message-State: AIkVDXJEDhUWJ9NoXOvz13W3aorebKtGMdx42rlWbS40KpxquwolU4W2ArnxEVwZMwg85Q==
X-Received: by 10.99.23.69 with SMTP id 5mr24562659pgx.76.1483484477387;
        Tue, 03 Jan 2017 15:01:17 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.157.6.227 with SMTP id 90ls27428495otx.34.gmail; Tue, 03 Jan
 2017 15:01:16 -0800 (PST)
X-Received: by 10.157.17.3 with SMTP id g3mr2992450ote.8.1483484476645;
        Tue, 03 Jan 2017 15:01:16 -0800 (PST)
In-Reply-To: <efdf2357-00a6-408a-8772-12f99135a880@isocpp.org>
X-Original-Sender: gmisocpp@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:30202
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/30202>

------=_Part_5498_84607148.1483484476010
Content-Type: multipart/alternative; 
	boundary="----=_Part_5499_667225547.1483484476010"

------=_Part_5499_667225547.1483484476010
Content-Type: text/plain; charset=UTF-8


How about:

enum class trafflic_light{ unknown, reg, yellow, green };

traffic_light = traffic_light::unknown; // My default.
bool success = make_enum(light, x); // Does not set light on failure. X of 
any integral type
if (!success)
 throw std::logic_error("Error: Expected a value compatible with a 
traffic_light enum.");
if (light == traffic_light::green)
 cpp20_you_are_our_only_hope();

// make_enum - Standard special funciton. Make this available in C too.


On Tuesday, January 3, 2017 at 10:56:05 PM UTC+13, 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.
>
> template <typename Enum, typename Int>
> optional<Enum> enum_cast(Int value);
>
> requires: is_enum<Enum>, is_integral<Int>
>
> enum color
> {
>    red = 1,
>    green = 200,
>    blue = 3
> };
>
> static_assert(enum_cast<color>(200u) == color::green);
> static_assert(enum_cast<color>(3) == color::blue);
> static_assert(enum_cast<color>(4) == nullopt);
>
>
> This should work for both scoped and unscoped enums.
> For opaque enums this would only check if value fits in underlying type.
>
> Why do we want this:
>  - validation of input data (e.g. coming from user or deserialization).
>  - other?
>
> I know this could be purely library extension if based on reflection, but 
> since we don't know when we will get reflection,
> and implementation based on reflection may not be optimal,
> I think it should be implemented with compiler support (i.e. via 
> __builtin_* intrinsic).
>
> Regards,
> Maciej
>

-- 
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/e6c43713-ad62-4a5e-8eda-23c59bb67dd9%40isocpp.org.

------=_Part_5499_667225547.1483484476010
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><br></div><div>How about:</div><div><br></div><p>enum=
 class trafflic_light{ unknown, reg, yellow, green };</p><div><br></div><di=
v>traffic_light =3D traffic_light::unknown; // My default.<br>bool success =
=3D make_enum(light, x); // Does not set light on failure. X of any integra=
l type<br>if (!success)<br>=C2=A0throw std::logic_error(&quot;Error: Expect=
ed a value=C2=A0compatible=C2=A0with a traffic_light enum.&quot;);<br>if (l=
ight =3D=3D traffic_light::green)<br>=C2=A0cpp20_you_are_our_only_hope();</=
div><div><br></div><div><div>// make_enum - Standard special funciton. Make=
 this available in C too.</div><br><br>On Tuesday, January 3, 2017 at 10:56=
:05 PM UTC+13, m.ce...@gmail.com wrote:</div><blockquote class=3D"gmail_quo=
te" style=3D"margin: 0px 0px 0px 0.8ex; padding-left: 1ex; border-left-colo=
r: rgb(204, 204, 204); border-left-width: 1px; border-left-style: solid;"><=
div dir=3D"ltr">Hi,<div><br></div><div>I propose to add a enum_cast functio=
n that safely casts int value to a target enum type if int value represents=
 a valid enumerator.</div><div><br></div><div style=3D"border: 1px solid rg=
b(187, 187, 187); border-image: none; -ms-word-wrap: break-word; background=
-color: rgb(250, 250, 250);"><code><div><span style=3D"color: rgb(0, 0, 136=
);">template</span><span style=3D"color: rgb(0, 0, 0);"> </span><span style=
=3D"color: rgb(102, 102, 0);">&lt;</span><span style=3D"color: rgb(0, 0, 13=
6);">typename</span><span style=3D"color: rgb(0, 0, 0);"> </span><span styl=
e=3D"color: rgb(102, 0, 102);">Enum</span><span style=3D"color: rgb(102, 10=
2, 0);">,</span><span style=3D"color: rgb(0, 0, 0);"> </span><span style=3D=
"color: rgb(0, 0, 136);">typename</span><span style=3D"color: rgb(0, 0, 0);=
"> </span><span style=3D"color: rgb(102, 0, 102);">Int</span><span style=3D=
"color: rgb(102, 102, 0);">&gt;</span><span style=3D"color: rgb(0, 0, 0);">=
<br>optional</span><span style=3D"color: rgb(102, 102, 0);">&lt;</span><spa=
n style=3D"color: rgb(102, 0, 102);">Enum</span><span style=3D"color: rgb(1=
02, 102, 0);">&gt;</span><span style=3D"color: rgb(0, 0, 0);"> enum_cast</s=
pan><span style=3D"color: rgb(102, 102, 0);">(</span><span style=3D"color: =
rgb(102, 0, 102);">Int</span><span style=3D"color: rgb(0, 0, 0);"> value</s=
pan><span style=3D"color: rgb(102, 102, 0);">);</span><span style=3D"color:=
 rgb(0, 0, 0);"><br><br>requires</span><span style=3D"color: rgb(102, 102, =
0);">:</span><span style=3D"color: rgb(0, 0, 0);"> is_enum</span><span styl=
e=3D"color: rgb(102, 102, 0);">&lt;</span><font color=3D"#000000"><span sty=
le=3D"color: rgb(102, 0, 102);">Enum</span></font><span style=3D"color: rgb=
(102, 102, 0);">&gt;,</span><span style=3D"color: rgb(0, 0, 0);"> is_integr=
al</span><span style=3D"color: rgb(102, 102, 0);">&lt;</span><span style=3D=
"color: rgb(102, 0, 102);">Int</span><span style=3D"color: rgb(102, 102, 0)=
;">&gt;</span><span style=3D"color: rgb(0, 0, 0);"><br><br></span><span sty=
le=3D"color: rgb(0, 0, 136);">enum</span><span style=3D"color: rgb(0, 0, 0)=
;"> color<br></span><span style=3D"color: rgb(102, 102, 0);">{</span><span =
style=3D"color: rgb(0, 0, 0);"><br>=C2=A0 =C2=A0red </span><span style=3D"c=
olor: rgb(102, 102, 0);">=3D</span><span style=3D"color: rgb(0, 0, 0);"> </=
span><span style=3D"color: rgb(0, 102, 102);">1</span><span style=3D"color:=
 rgb(102, 102, 0);">,</span><span style=3D"color: rgb(0, 0, 0);"><br>=C2=A0=
 =C2=A0green </span><span style=3D"color: rgb(102, 102, 0);">=3D</span><spa=
n style=3D"color: rgb(0, 0, 0);"> </span><span style=3D"color: rgb(0, 102, =
102);">200</span><span style=3D"color: rgb(102, 102, 0);">,</span><span sty=
le=3D"color: rgb(0, 0, 0);"><br>=C2=A0 =C2=A0blue </span><span style=3D"col=
or: rgb(102, 102, 0);">=3D</span><span style=3D"color: rgb(0, 0, 0);"> </sp=
an><span style=3D"color: rgb(0, 102, 102);">3</span><span style=3D"color: r=
gb(0, 0, 0);"><br></span><span style=3D"color: rgb(102, 102, 0);">};</span>=
<span style=3D"color: rgb(0, 0, 0);"><br><br></span><span style=3D"color: r=
gb(0, 0, 136);">static_assert</span><span style=3D"color: rgb(102, 102, 0);=
">(</span><span style=3D"color: rgb(0, 0, 0);">enum_cast</span><span style=
=3D"color: rgb(0, 136, 0);">&lt;color&gt;</span><span style=3D"color: rgb(1=
02, 102, 0);"><wbr>(</span><font color=3D"#006666"><span style=3D"color: rg=
b(0, 102, 102);">200u</span></font><span style=3D"color: rgb(102, 102, 0);"=
>)</span><span style=3D"color: rgb(0, 0, 0);"> </span><span style=3D"color:=
 rgb(102, 102, 0);">=3D=3D</span><span style=3D"color: rgb(0, 0, 0);"> colo=
r</span><span style=3D"color: rgb(102, 102, 0);">::</span><font color=3D"#0=
00000"><span style=3D"color: rgb(0, 0, 0);">green</span></font><span style=
=3D"color: rgb(102, 102, 0);">);</span><span style=3D"color: rgb(0, 0, 0);"=
><br></span><span style=3D"color: rgb(0, 0, 136);">static_assert</span><spa=
n style=3D"color: rgb(102, 102, 0);">(</span><span style=3D"color: rgb(0, 0=
, 0);">enum_cast</span><span style=3D"color: rgb(0, 136, 0);">&lt;color&gt;=
</span><span style=3D"color: rgb(102, 102, 0);"><wbr>(</span><span style=3D=
"color: rgb(0, 102, 102);">3</span><span style=3D"color: rgb(102, 102, 0);"=
>)</span><span style=3D"color: rgb(0, 0, 0);"> </span><span style=3D"color:=
 rgb(102, 102, 0);">=3D=3D</span><span style=3D"color: rgb(0, 0, 0);"> colo=
r</span><span style=3D"color: rgb(102, 102, 0);">::</span><span style=3D"co=
lor: rgb(0, 0, 0);">blue</span><span style=3D"color: rgb(102, 102, 0);">);<=
/span><span style=3D"color: rgb(0, 0, 0);"><br></span><span style=3D"color:=
 rgb(0, 0, 136);">static_assert</span><span style=3D"color: rgb(102, 102, 0=
);">(</span><span style=3D"color: rgb(0, 0, 0);">enum_cast</span><span styl=
e=3D"color: rgb(0, 136, 0);">&lt;color&gt;</span><span style=3D"color: rgb(=
102, 102, 0);"><wbr>(</span><span style=3D"color: rgb(0, 102, 102);">4</spa=
n><span style=3D"color: rgb(102, 102, 0);">)</span><span style=3D"color: rg=
b(0, 0, 0);"> </span><span style=3D"color: rgb(102, 102, 0);">=3D=3D</span>=
<span style=3D"color: rgb(0, 0, 0);"> nullopt</span><span style=3D"color: r=
gb(102, 102, 0);">);</span><span style=3D"color: rgb(0, 0, 0);"><br><br></s=
pan></div></code></div><div><br></div><div>This should work for both scoped=
 and unscoped enums.</div><div>For opaque enums this would only check if va=
lue fits in underlying type.<br></div><div><br></div><div>Why do we want th=
is:<br></div><div>=C2=A0- validation of input data (e.g. coming from user o=
r deserialization).</div><div>=C2=A0- other?</div><div><br></div><div>I kno=
w this could be purely library extension if based on reflection, but since =
we don&#39;t know when we will get reflection,</div><div>and implementation=
 based on reflection may not be optimal,</div><div>I think it should be imp=
lemented with compiler support (i.e. via __builtin_* intrinsic).</div><div>=
<br></div><div>Regards,</div><div>Maciej</div></div></blockquote></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/e6c43713-ad62-4a5e-8eda-23c59bb67dd9%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/e6c43713-ad62-4a5e-8eda-23c59bb67dd9=
%40isocpp.org</a>.<br />

------=_Part_5499_667225547.1483484476010--

------=_Part_5498_84607148.1483484476010--

.
