220 30163 <efdf2357-00a6-408a-8772-12f99135a880@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: m.cencora@gmail.com
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: enum_cast proposal
Date: Tue, 3 Jan 2017 01:56:05 -0800 (PST)
Lines: 179
Approved: news@gmane.org
Message-ID: <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_2059_164701516.1483437365479"
X-Trace: blaine.gmane.org 1483437369 19587 195.159.176.226 (3 Jan 2017 09:56:09 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Tue, 3 Jan 2017 09:56:09 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBD7YFUMJRYMBBNPKVXBQKGQEZ6RV4HY@isocpp.org Tue Jan 03 10:56:05 2017
Return-path: <std-proposals+bncBD7YFUMJRYMBBNPKVXBQKGQEZ6RV4HY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-yb0-f199.google.com ([209.85.213.199])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBD7YFUMJRYMBBNPKVXBQKGQEZ6RV4HY@isocpp.org>)
	id 1cOLp4-0004Zg-IF
	for gclcip-std-proposals@m.gmane.org; Tue, 03 Jan 2017 10:56:02 +0100
Original-Received: by mail-yb0-f199.google.com with SMTP id n141sf237243589ybf.0
        for <gclcip-std-proposals@m.gmane.org>; Tue, 03 Jan 2017 01:56:06 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:message-id: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=WrsZGnL8rl4QRG/1ZMF9z0ZNopndY1CWpGW+ENhZVtU=;
        b=tF8LAKfTVXsXZ3ensEez3JP6dqcDspUXDvhpn4sJN678JDfkMOlXx+mw1agSWxDPRV
         lxjnJCLCsHdLkTDo1QzUOaPtFkhYfU8LEDm27RuE3EDMg/SIXbkMSTMpuJwkeNX9/ahM
         OZoEve+07b+DrfbDyeG1Ihy+VD9VOFDf9/TOW1kwK+bjnBp1ZfDhUVriffZ20uLXZzYG
         ALPlx3GPZBkwhFXGXkmAJMPY5QRABwVyJR+oGkp93GDgk4Mdtq952gLi2dE9ET+haGHO
         WmKfcMmcXYF58d8JhGjPfrgIkHgfk5BkCFYZlCJFbZU/z9fEjrzok9xTOEEdMdiKh6eC
         CAlA==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=date:from:to:message-id: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=WrsZGnL8rl4QRG/1ZMF9z0ZNopndY1CWpGW+ENhZVtU=;
        b=YYISvKKdQxABJJWnQP40DmvxZY+xmz4G3zNmLbKCh8JgEufq55k8eXByqWzd1T7MEu
         iIzMfIbrjqOORD3YHbVwUGc5gvZTCF2PDiCMJN+kko5Fn/ITvX0ebv1sERX9eWHX/W5Q
         32yWbAYuRLdrHGxKj5FRm/JY9aX0uQQygIW1zI+D8RY/LIkQAeNoqmENHVrd4qu0J9EW
         BOO3+qcFR1TvzLz7uZ5nVTA9taHUyDqM8lA3lwnbdMQ/PUUo+zFyDAdgQH0z+YBBaEFA
         Os3LzY/pqDW10Ni68XsuxQSRrvpX3ChDlw3GS9aPmd80a3XdlmpAeeoerklr7oDDHa86
         98cQ==
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:message-id: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=WrsZGnL8rl4QRG/1ZMF9z0ZNopndY1CWpGW+ENhZVtU=;
        b=dEVbzmCGuyfFSkVZNW2HDtt1duz7nv6DDnWnzl5E0YwoBTj1IAmp3SXVvskAVazXiR
         QnlpToGtxF815ZREPfn9VCtInFvd/uuLbO4EhT0ykrglauB+i9FTtoJ+GCq93lE/BcOc
         b5PceX6GjQLjJQ0AlkTE/DgYEKM5CVAQtN4MKUw1NIVULDJfYRMymLIinmp0FmFos7oU
         rLst7D26VuVQq9raNtxW2s57xlABJEczFmn3MYmDvZVE/N790bKDJqldSvgaMnHWkD3T
         hbAtoYmoq7bIfiruqXmrZFawKTTZsDGkiee/3HsmTRInAQg9WtB2W1axonFnB33y7rrS
         enew==
X-Gm-Message-State: AIkVDXIrB+8T/hId2Q+CIb0Vrt+EgcPJFtazyEZPcT2I+f0FRKQ7XTD2OgUx8lYCGp/o+w==
X-Received: by 10.129.166.216 with SMTP id d207mr19537737ywh.120.1483437366415;
        Tue, 03 Jan 2017 01:56:06 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.157.15.106 with SMTP id 97ls41610363ott.11.gmail; Tue, 03 Jan
 2017 01:56:05 -0800 (PST)
X-Received: by 10.157.49.66 with SMTP id v2mr2715240otd.19.1483437365827;
        Tue, 03 Jan 2017 01:56:05 -0800 (PST)
X-Original-Sender: m.cencora@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:30163
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/30163>

------=_Part_2059_164701516.1483437365479
Content-Type: multipart/alternative; 
	boundary="----=_Part_2060_263266050.1483437365480"

------=_Part_2060_263266050.1483437365480
Content-Type: text/plain; charset=UTF-8

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/efdf2357-00a6-408a-8772-12f99135a880%40isocpp.org.

------=_Part_2060_263266050.1483437365480
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi,<div><br></div><div>I propose to add a enum_cast functi=
on that safely casts int value to a target enum type if int value represent=
s a valid enumerator.</div><div><br></div><div class=3D"prettyprint" style=
=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-word; background=
-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=3D"subp=
rettyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">templ=
ate</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span=
 style=3D"color: #008;" class=3D"styled-by-prettify">typename</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"c=
olor: #606;" class=3D"styled-by-prettify">Enum</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"st=
yled-by-prettify">typename</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-pre=
ttify">Int</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
&gt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>op=
tional</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;=
</span><span style=3D"color: #606;" class=3D"styled-by-prettify">Enum</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify"> enum_cast</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D=
"color: #606;" class=3D"styled-by-prettify">Int</span><span style=3D"color:=
 #000;" class=3D"styled-by-prettify"> value</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">);</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"><br><br>requires</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">:</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> is_enum</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">&lt;</span><font color=3D"#000000"><span style=3D"c=
olor: #606;" class=3D"styled-by-prettify">Enum</span></font><span style=3D"=
color: #660;" class=3D"styled-by-prettify">&gt;,</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> is_integral</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #6=
06;" class=3D"styled-by-prettify">Int</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"><br><br></span><span style=3D"color: #008;" class=3D"s=
tyled-by-prettify">enum</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> color<br></span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"><br>=C2=A0 =C2=A0red </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: #066;" class=3D"styled-by-prettify">1=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0gr=
een </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span=
 style=3D"color: #066;" class=3D"styled-by-prettify">200</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0blue </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #0=
66;" class=3D"styled-by-prettify">3</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">};</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"><br><br></span><span style=3D"color: #008;" class=3D"styled-by-p=
rettify">static_assert</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y">enum_cast</span><span style=3D"color: #080;" class=3D"styled-by-prettify=
">&lt;color&gt;</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">(</span><font color=3D"#006666"><span style=3D"color: #066;" class=3D"=
styled-by-prettify">200u</span></font><span style=3D"color: #660;" class=3D=
"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">=3D=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
color</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</s=
pan><font color=3D"#000000"><span style=3D"color: #000;" class=3D"styled-by=
-prettify">green</span></font><span style=3D"color: #660;" class=3D"styled-=
by-prettify">);</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"><br></span><span style=3D"color: #008;" class=3D"styled-by-prettify">s=
tatic_assert</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">enum_ca=
st</span><span style=3D"color: #080;" class=3D"styled-by-prettify">&lt;colo=
r&gt;</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</sp=
an><span style=3D"color: #066;" class=3D"styled-by-prettify">3</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">=3D=3D</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"> color</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify">blue</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">);</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"><br></span><span style=3D"color: #008;" class=3D"styled-by-prettify">st=
atic_assert</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">enum_cas=
t</span><span style=3D"color: #080;" class=3D"styled-by-prettify">&lt;color=
&gt;</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</spa=
n><span style=3D"color: #066;" class=3D"styled-by-prettify">4</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">)</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">=3D=3D</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> nullopt</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">);</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"><br><br></span></div></code></div><div><br></div><div>Th=
is should work for both scoped and unscoped enums.</div><div>For opaque enu=
ms this would only check if value fits in underlying type.<br></div><div><b=
r></div><div>Why do we want this:<br></div><div>=C2=A0- validation of input=
 data (e.g. coming from user or deserialization).</div><div>=C2=A0- other?<=
/div><div><br></div><div>I know this could be purely library extension if b=
ased 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 implemented with compiler support (i.e. via __bu=
iltin_* intrinsic).</div><div><br></div><div>Regards,</div><div>Maciej</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/efdf2357-00a6-408a-8772-12f99135a880%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/efdf2357-00a6-408a-8772-12f99135a880=
%40isocpp.org</a>.<br />

------=_Part_2060_263266050.1483437365480--

------=_Part_2059_164701516.1483437365479--

.
